tazboy
tazboy

Reputation: 1754

BeautifulSoup loop issues - ResultSet object has no attribute '%s

The code below works:

for i in range(0,1):
    url = "https://www.blah.com/" + get_chapter(chron_list[i])
    book = requests.get(url, headers=headers)
    c = book.content
    soup = soup(c, "html.parser")
    print(soup.prettify())

If I change to range(1,2) it also works since I have two items in the chron_list. But when I change it to range(0,2) it no longer works and gives me the following error:

"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. 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 items like a single item. Did you call find_all() when you meant to call find()?

I don't understand why it works for a range of one but not for a range of two. I thought maybe it was a timing issue so I added a delay of five seconds before it continued to the next iteration but that didn't work.

Upvotes: 0

Views: 564

Answers (1)

Blender
Blender

Reputation: 298206

You redefine soup within your code. After two iterations of your loop, soup will not refer to BeautifulSoup but instead to the object returned by soup(c, "html.parser"):

soup = soup(c, "html.parser")
^^^^^^^^^^^

Don't rename BeautifulSoup (or if you do, choose a different name):

from bs4 import BeautifulSoup

...

soup = BeautifulSoup(c, "html.parser")

Upvotes: 4

Related Questions