调用函数find()beautifulsoup时结果不完整

2024-06-01 11:10:14 发布

您现在位置:Python中文网/ 问答频道 /正文

为了研究的目的,我试图爬过这个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的全部内容?你知道吗


Tags: ofindivanurlresponsearticleopener
1条回答
网友
1楼 · 发布于 2024-06-01 11:10:14
soup = BeautifulSoup(response, 'lxml')

html.parser是不稳定的,非常不受推荐,默认情况下,bs4使用lxml,让我们继续使用默认值。你知道吗

相关问题 更多 >