Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.findAll()
#7
(Nov-16-2018, 12:21 AM)Truman Wrote: snippsat, I get this when I try to use findAll.
find_all()(this is what you should use not CamelCase) return a list,so can not use .text on list.
Now all red text is in a list.
import requests
from bs4 import BeautifulSoup

html = requests.get("http://www.pythonscraping.com/pages/warandpeace.html")
soup = BeautifulSoup(html.content, 'html.parser')
red_text = soup.find_all('span', class_="red")
print(red_text)
So to take out a part.
>>> red_text[3]
<span class="red">First of all, dear friend, tell me how you are. Set your friend's
mind at rest,</span>

# Print out text of part 3
>>> print(red_text[3].text)
First of all, dear friend, tell me how you are. Set your friend's
mind at rest,
To print all red text.
>>> for all_red in red_text:
...     print(all_red.text)
...     
Well, Prince, so Genoa and Lucca are now just family estates of the
Buonapartes. But I warn you, if you don't tell me that this means war,
... ect
Quote:Also, your code does a different thing. It doesn't pick up word from id attribute.
Your first code is wrong there is no id="title" in source code on that web page.
The only id is <div id="text"> this is all text on page.
You have to read source code for web site you parse.
Reply


Messages In This Thread
.findAll() - by Truman - Nov-14-2018, 10:42 PM
RE: .findAll() - by Gribouillis - Nov-14-2018, 10:49 PM
RE: .findAll() - by Truman - Nov-14-2018, 10:52 PM
RE: .findAll() - by Gribouillis - Nov-14-2018, 11:05 PM
RE: .findAll() - by Truman - Nov-16-2018, 12:21 AM
RE: .findAll() - by snippsat - Nov-14-2018, 11:37 PM
RE: .findAll() - by snippsat - Nov-16-2018, 12:51 PM
RE: .findAll() - by Truman - Nov-17-2018, 12:08 AM
RE: .findAll() - by snippsat - Nov-17-2018, 01:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  re.findall help searching for string in xml response mugster 2 3,276 May-30-2018, 03:27 PM
Last Post: mugster
  Different Output of findall and search in re module shiva 1 2,346 Mar-12-2018, 08:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020