使用BeautifulSoup进行网络爬网

时间:2020-11-04 14:57:09

标签: python beautifulsoup web-crawler

如何提取

段落标记内以及命名类下的所有数据?我从一个类似问题的答案中得到了启发:

Web crawling using python beautifulsoup

并提出了以下代码:

import requests
from bs4 import BeautifulSoup
source = requests.get('https://www.bbc.com/news/election-us-2020-54807899').text

soup = BeautifulSoup(source, 'lxml')
article = soup.find('div')
ps = article.findall('p')
head = article.findall('h1')

for p in ps:
    print(p.text)
for h in head:
    print(h.text)

但是我得到一个错误:TypeError:'NoneType'对象不可调用。有谁知道我如何使它成功爬网?谢谢!

2 个答案:

答案 0 :(得分:1)

实际上是find_all,而不是findall。这是更正的代码:

import requests
from bs4 import BeautifulSoup
source = requests.get('https://www.bbc.com/news/election-us-2020-54807899').text

soup = BeautifulSoup(source, 'lxml')
article = soup.find('div')
ps = article.find_all('p')
head = article.find_all('h1')

for p in ps:
    print(p.text)
for h in head:
    print(h.text)

输出:

Anthony ZurcherNorth America reporter@awzurcheron Twitter
Election night in the US is on the verge of turning into election week. Here's what the candidates need to happen in order to win the White House.
Polls before election day suggested possible outcomes ranging from a comfortable win for Joe Biden, to a narrow Donald Trump victory where he loses the national popular vote but once again carries enough battleground states to win in the electoral college.
After a vote count that has stretched from the evening into the early morning hours, a Biden landslide is off the table. The victory will be narrow. It's just a matter of who and how - and how long until we know.
Donald Trump has already declared victory and accused his opponents of committing electoral fraud. This is, however, simply not the case at this point. There are still millions of legally cast ballots in the process of being counted.
A national race is boiling down to just a handful of states: Arizona, Georgia, Wisconsin, Michigan and Pennsylvania.
Arizona appears to be trending toward Biden, which means the Democrat would have to win two of the three so-called 2016 "blue wall" states - Wisconsin, Michigan and Pennsylvania - to secure victory.
Biden trails in all three, but the remaining votes to be counted will favour the Democrat - perhaps by enough to pull him ahead.
There are more than 1.4 million vote-by-mail ballots left to count in Pennsylvania, and it could take days to tabulate them all. The big cities in Michigan (Detroit) and Wisconsin (Milwaukee) are also yet to fully report, and they will heavily tilt toward the Democrats.
Meanwhile, Georgia is a wildcard. What seemed heading towards a comfortable Trump win earlier on Tuesday has ended up in a dead heat. Results from Biden-friendly Atlanta was, improbably enough, delayed by a broken water pipe in a vote-counting location. 
A Democratic win in Georgia would mean Biden only has to carry one of those Midwest states.
What was once a nightmare scenario is taking shape, with Biden claiming he is on a path to victory and Trump lobbing unfounded accusations of voter fraud and electoral theft.
It's a recipe for acrimony and a protracted court battle, which ends with supporters on the losing side feeling angry and cheated.
Although the final results aren't yet known, what is clear on election night is that the US continues to be a sharply divided nation. The American voters did not repudiate Trump in any meaningful way. Nor did they give him the kind of ringing endorsement that the president had hoped for.
Instead, the battle lines are drawn - and the political warfare will continue no matter who prevails in this particular election.
US Election 2020: Results and exit poll in maps and charts
Could Trump contest the result? 
playElection results: The story of the night so far...
Knife-edge US vote hangs on a few key states
The US president outperforms pollsters' predictions but the race remains tight in key battlegrounds.
Tense wait as US election winner remains unclear
Democrats face uphill struggle for US Senate
India's longest burning gas fire is destroying lives
'Young black people need role models to see' Video'Young black people need role models to see'
What does developing a Covid-19 vaccine look like? VideoWhat does developing a Covid-19 vaccine look like?
In pictures: Trump and Biden through the years
Rescuers rush to save beached pilot whales. VideoRescuers rush to save beached pilot whales
Finding positivity in lockdown through photography
Bushfire koalas going back to the wild. VideoBushfire koalas going back to the wild
Three elders reveal what it takes to be a leader. VideoThree elders reveal what it takes to be a leader
Van Gogh suffered 'episodes of delirium'
US Election 2020: Results and exit poll in maps and charts1
US Election 2020: Tense wait as US election winner remains unclear2
US election 2020 polls: Who is ahead - Trump or Biden?3
US election 2020: When will we know the result?4
US election 2020: Why don't we know the result yet?5
Arnab Goswami: Republic TV founder arrested in suicide case6
US election results: What Trump and Biden still need to clinch victory7
US Election 2020: Democrats face tight race for control of Senate8
US election 2020: Beer and angst as White House party defies another protocol9
New Zealand: Book pulled after author criticises Maori tattoo10
© 2020 BBC. The BBC is not responsible for the content of external sites. Read about our approach to external linking.
US election results: What Trump and Biden still need to clinch victory

答案 1 :(得分:0)

它将是findAllfind_all而不是findall

import requests
from bs4 import BeautifulSoup
source = requests.get('https://www.bbc.com/news/election-us-2020-54807899').text

soup = BeautifulSoup(source, 'lxml')
article = soup.find('div')
ps = article.findAll('p')
head = article.findAll('h1')

for p in ps:
    print(p.text)
for h in head:
    print(h.text)

# output 

Anthony ZurcherNorth America reporter@awzurcheron Twitter
Election night in the US is on the verge of turning into election week. Here's what the candidates need to happen in order to win the White House.
Polls before election day suggested possible outcomes ranging from a comfortable win for Joe Biden, to a narrow Donald Trump victory where he loses the national popular vote but once again carries enough battleground states to win in the electoral college.
After a vote count that has stretched from the evening into the early morning hours, a Biden landslide is off the table. The victory will be narrow. It's just a matter of who and how - and how long until we know.
Donald Trump has already declared victory and accused his opponents of committing electoral fraud. This is, however, simply not the case at this point. There are still millions of legally cast ballots in the process of being counted.
A national race is boiling down to just a handful of states: Arizona, Georgia, Wisconsin, Michigan and Pennsylvania.
Arizona appears to be trending toward Biden, which means the Democrat would have to win two of the three so-called 2016 "blue wall" states - Wisconsin, Michigan and Pennsylvania - to secure victory.
Biden trails in all three, but the remaining votes to be counted will favour the Democrat - perhaps by enough to pull him ahead.
There are more than 1.4 million vote-by-mail ballots left to count in Pennsylvania, and it could take days to tabulate them all. The big cities in Michigan (Detroit) and Wisconsin (Milwaukee) are also yet to fully report, and they will heavily tilt toward the Democrats.
Meanwhile, Georgia is a wildcard. What seemed heading towards a comfortable Trump win earlier on Tuesday has ended up in a dead heat. Results from Biden-friendly Atlanta was, improbably enough, delayed by a broken water pipe in a vote-counting location. 
A Democratic win in Georgia would mean Biden only has to carry one of those Midwest states.
What was once a nightmare scenario is taking shape, with Biden claiming he is on a path to victory and Trump lobbing unfounded accusations of voter fraud and electoral theft.
It's a recipe for acrimony and a protracted court battle, which ends with supporters on the losing side feeling angry and cheated.
Although the final results aren't yet known, what is clear on election night is that the US continues to be a sharply divided nation. The American voters did not repudiate Trump in any meaningful way. Nor did they give him the kind of ringing endorsement that the president had hoped for.
Instead, the battle lines are drawn - and the political warfare will continue no matter who prevails in this particular election.
US Election 2020: Results and exit poll in maps and charts
Could Trump contest the result? 
playElection results: The story of the night so far...
Knife-edge US vote hangs on a few key states
The US president outperforms pollsters' predictions but the race remains tight in key battlegrounds.
Tense wait as US election winner remains unclear
Democrats face uphill struggle for US Senate
India's longest burning gas fire is destroying lives
'Young black people need role models to see' Video'Young black people need role models to see'
What does developing a Covid-19 vaccine look like? VideoWhat does developing a Covid-19 vaccine look like?
In pictures: Trump and Biden through the years
Rescuers rush to save beached pilot whales. VideoRescuers rush to save beached pilot whales
Finding positivity in lockdown through photography
Bushfire koalas going back to the wild. VideoBushfire koalas going back to the wild
Three elders reveal what it takes to be a leader. VideoThree elders reveal what it takes to be a leader
Van Gogh suffered 'episodes of delirium'
US Election 2020: Results and exit poll in maps and charts1
US Election 2020: Tense wait as US election winner remains unclear2
US election 2020 polls: Who is ahead - Trump or Biden?3
US election 2020: When will we know the result?4
US election 2020: Why don't we know the result yet?5
Arnab Goswami: Republic TV founder arrested in suicide case6
US election results: What Trump and Biden still need to clinch victory7
US Election 2020: Democrats face tight race for control of Senate8
US election 2020: Beer and angst as White House party defies another protocol9
New Zealand: Book pulled after author criticises Maori tattoo10
© 2020 BBC. The BBC is not responsible for the content of external sites. Read about our approach to external linking.
US election results: What Trump and Biden still need to clinch victory