-1

enter image description here

import requests

from bs4 import BeautifulSoup, NavigableString, Tag

url = "https://www.codewithharry.com"

r = requests.get(url)

htmlContent = r.content

soup = BeautifulSoup(htmlContent, 'html.parser')

print(soup.find('p')['class'])
James Z
  • 12,209
  • 10
  • 24
  • 44
  • What are you trying to accomplish? – Yarin_007 Oct 02 '22 at 03:25
  • I want to find the class used in that paragraph and the output is shown also but there is a error shown that the literal class cannot be assigned to the supportsIndex so I want to know why it is showing –  Oct 02 '22 at 03:27
  • Okay, what does `soup.find('p')` give? – Yarin_007 Oct 02 '22 at 03:29
  • It give me the first paragraph of the website –  Oct 02 '22 at 03:30
  • What does `soup.find('p').attrs` give? – Yarin_007 Oct 02 '22 at 03:34
  • it gives an error saying that 'attrs' is not a known member of "none" and with the error it also give me the result "{'class': ['mt-2', 'text-sm', 'text-gray-500', 'md:text-base']}" –  Oct 02 '22 at 03:37
  • strange? Perhaps attach screenshots to the question. Anyway, try `first_p=soup.find('p'); print(first_p.get('class'))` – Yarin_007 Oct 02 '22 at 03:40
  • It is showing the same output as before "['mt-2', 'text-sm', 'text-gray-500', 'md:text-base']" but giving the error as **cannot access member for type NavigableString** –  Oct 02 '22 at 04:17

1 Answers1

1

This code showed me a warning in vs code for the written code and the output that was given was correct so you can just write there as shown below:

print (soup. Find('p')['class']) # type: ignore

Sometimes it show the warning in the terminal in vs code so you can use this to stop the error