Getting a AttributeError: ResultSet object has no attribute 'prettify'. while using BeautifulSoup

Viewed 380

I am trying to get the html of the page but it is showing↓ for the second value of the list .

Traceback (most recent call last):
  File "E:\mycodes\python codes\testcode.py", line 14, in <module>
    print(soup.prettify())
  File "C:\Users\sonug\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\element.py", line 2161, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'prettify'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

and it is successfully printing the html code of the first page just not printing for the second value of the list .

HERE IS MY CODE:-

from bs4 import BeautifulSoup as soup
import requests

USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
session = requests.Session()
session.headers['User-Agent'] = USER_AGENT

query=['who is the president of India','who is the president of usa']

for query in query:
    url=f"https://www.google.com/search?q={query}"
    html = session.get(url).text
    soup = soup(html,'html.parser')
    print(soup.prettify())
2 Answers

In your first iteration, you overwrite your BeautfulSoup, which you named soup, with the output of calling it on the html. In the second iteration, the variable soup no longer points to BeautifulSoup but instead points to output from the first iteration, so things just get messed up.

from bs4 import BeautifulSoup as soup
import requests

USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
session = requests.Session()
session.headers['User-Agent'] = USER_AGENT

queries=['who is the president of India','who is the president of usa']

for query in queries:
    url=f"https://www.google.com/search?q={query}"
    html = session.get(url).text
    soup_result = soup(html,'html.parser')
    print(soup_result.prettify())

Instead you using soup in your result set you should use another variable in place of that.

Like result or something else.

Try this:-

result = soup(html,'html.parser')

Related