添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I created a simple scraper but I have a problem with changing ResultSet to text. I want to get the only text without a href etc. When I use find method it works good, but when I add find_all as a second method it shows error:

numberone = soup.find("span", itemprop="house").text                      <---- this works good
print(numberone)
numbertwo = soup.find("div", class_="interesting").find_all('a').text     <---- this does not work
print(numbertwo)

Output for 'numberone' is good, but for 'numbertwo' it shows an error:

"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 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

Maybe someone knows the solution?

How come? It literally says You're probably treating a list of elements like a single element, how is that not enough? I'm confused... – AMC Feb 15, 2020 at 20:44

it is because it returns a set of results, in order to print all the results you can use a for-loop like this:

results = numbertwo = soup.find("div", class_="interesting").find_all('a')
for result in results:
    print(result.text)
                That link refers to SQLAlchemy database result sets, not a BeatifulSoup resultset (which is a subclass of list). They are very different beasts.
– Martijn Pieters
                Feb 16, 2020 at 2:43