(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.