【问题标题】:Incomplete result when calling the function find() beautifulsoup调用函数 find() beautifulsoup 时结果不完整
【发布时间】:2017-02-15 15:22:01
【问题描述】:

出于研究目的,我正在尝试抓取此 url,但结果不完整:

opener = urllib.request.build_opener()
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
response = opener.open(url)
soup = BeautifulSoup(response, 'html.parser')
article = soup.find("div", { "class" : "entry" })
print(article)

它给了我这个结果:

<div class="entry">
<header><strong>Racial Forensics in an Age of Race Denial</strong></header></div>

但是当我查看页面的代码源时,我可以看到更多:

<div class="entry">
<header><strong>Racial Forensics in an Age of Race Denial</strong></p>
</header>
<p>Austen Layard<br/>
<a href="http://www.theoccidentalobserver.net/2014/01/racial-forensics-in-an-age-of-race-denial/"><strong>Occidental Observer</strong></a><br/>
February 3, 2014</p>
....
</div>

此 URL 与我处理的许多其他 URL 之间的区别在于标签 header 的存在。

这个标签是我问题的根源吗? 如何检索标签div的全部内容?

【问题讨论】:

  • @MYGz 也不起作用

标签: python html python-3.x beautifulsoup web-crawler


【解决方案1】:
soup = BeautifulSoup(response, 'lxml')

html.parser 不稳定,非常不推荐,默认情况下,bs4 使用lxml,让我们保持默认。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2018-02-09
    • 2019-09-28
    • 2015-05-10
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多