【问题标题】:Unable to understand empty array output while using Beautiful Soup使用 Beautiful Soup 时无法理解空数组输出
【发布时间】:2020-03-03 19:34:32
【问题描述】:

我编写了一个非常小的 Python 脚本,用于从 CNN 网站上抓取文章标题。

import requests
from bs4 import BeautifulSoup

url='https://edition.cnn.com/'
topics=['world','politics','business']
r=requests.get(url+topics[1])
soup=BeautifulSoup(r.content,'html.parser')
spans=soup.find_all('span',{'class':"cd__headline-text"})
print(spans)

执行此代码后,我只是得到一个空列表作为输出。这不是我所期望或寻找的,因为我试图刮掉标签后面的文本。我试图引用的 html 块的 sn-p 是-

<span class="cd__headline-text">
Bernie Sanders faces pivotal clash as Democratic establishment joins forces against him
</span>

请帮助澄清我的代码似乎做错了什么和/或我可能犯的任何逻辑错误。

【问题讨论】:

  • 在使用requests 获取网站内容时要始终检查的几件事。你检查过网站的回复吗? r 看起来像您期望的那样吗?那么,在尝试查找其中的任何内容之前,您是否检查过soup 的内容?这两项检查可以告诉您您的get 是否成功,以及该站点是完全以 html 加载,还是在访问时异步加载(后者可能与 CNN 一起),在这种情况下,您需要一个类似的工具selenium 浏览器自动化
  • 嗨@G.Anderson!感谢您的答复 。我对我们报废比较陌生,所以我不确定异步加载意味着什么。你能详细说明一下吗?
  • 可能值得快速 google,但高级概述:像 Ajax(异步 Java 和 XML)这样的框架仅在 Web 浏览器访问页面时动态加载页面。这既可以定制用户体验,又可以防止像我们不幸的网络抓取之类的事情。检查你的soup,我敢打赌你只会看到几个 HTML 元素,因为除非浏览器点击它,否则页面的其余部分永远不会真正加载。
  • 如果您的问题得到解决,请将答案标记为已接受,以便其他人可以看到您的问题已得到解答。

标签: python web-scraping beautifulsoup


【解决方案1】:

您的代码运行良好。它只是不会为 politics 页面产生结果。

试试这个:

import requests
from bs4 import BeautifulSoup

url='https://edition.cnn.com/'
topics = ['world','politics','business']

headlines = []

for topic in topics:

    r = requests.get(url+topic)
    soup=BeautifulSoup(r.content,'html.parser')

    for span in soup.find_all('span',{'class':"cd__headline-text"}):
        headlines.append(span.text)
        print(span.text)
        print()

headlines 打印到:

The bizarre ways that coronavirus is changing etiquette
Over half of all virus cases in one country are linked to this group
Trump's Middle East plan could jeopardize Jordan-Israel peace treaty, Jordan PM says
Irish duo's win marks rare victory for women in the 'Nobel of architecture'
After more than 240 days, Australia's New South Wales is finally free from bushfires
Child drowns off Greek coast after Turkey opens border with Europe 
A migration crisis and disagreement with Turkey is the last thing Europe needs right now
Vatican to open controversial WW2-era files on Pope Pius XII
Netanyahu projected to win Israeli election, but exit polls suggest bloc just short of majority
Adviser to Iran's Supreme Leader dies after contracting coronavirus
Israeli election exit polls project Netanyahu in lead
She became pregnant at the age of 12. Now, Kenya's Christine Ongare is an Olympic boxing qualifier
Nigeria says it is ready and more than capable of dealing with coronavirus
Kenya bans commercial slaughter of donkeys following a rise in animal theft 
Violence forces Haiti to cancel Carnival
....

您不会得到politics 的结果,因为内容是在浏览器中使用Javascript 动态呈现的(正如G. Anderson 在他的cmets 中解释的那样)。但是,使用requests,您只能获得原始 HTML。

在浏览器中打开该站点并将View page sourceInspect element 进行比较。前者产生原始 HTML,后者产生呈现的 HTML。

【讨论】:

  • 谢谢,这确实解决了问题。进一步澄清,使用 Selenium 抓取动态渲染的内容更好还是我应该坚持漂亮的汤
  • 不客气。 ?关于您的问题:根据我的经验,这在很大程度上取决于。如果我可以在没有 Selenium 的情况下进行抓取,我通常会采用这种方式,因为使用 requests/BS4 进行抓取要快得多,即使我可能会在解析上投入更多时间。如果无法避免,我只会使用 Selenium。同时,Selenium 在文档中得到了很好的维护,并且工作得非常好。我建议你尝试这两种选择。这绝对是值得的,可能只是为了您获得的体验。祝你的项目好运!
猜你喜欢
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 2015-11-08
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多