gpt4 book ai didi

Python 请求与 urllib2

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:40 25 4
gpt4 key购买 nike

我已经多次使用 requests 库,我知道它有很多优点。但是,我试图检索以下维基百科页面:

https://en.wikipedia.org/wiki/Talk:Land_value_tax

并且 requests.get 部分检索它:

response = requests.get('https://en.wikipedia.org/wiki/Talk:Land_value_tax', verify=False)
html = response.text

我尝试使用 urllib2 和 urllib2.urlopen 并完全检索相同的页面:

html = urllib2.urlopen('https://en.wikipedia.org/wiki/Talk:Land_value_tax').read()

有谁知道为什么会发生这种情况以及如何使用请求解决它?

顺便说一句,看看这篇文章的浏览次数,我意识到人们很想知道这两个库之间的区别。如果有人知道这两个库之间的其他差异,如果他们编辑此问题或发布答案并添加这些差异,我将不胜感激。

最佳答案

在我看来,问题在于目标页面上的脚本。 js 驱动的内容在此处呈现(尤其是我发现对 mediawiki 的调用)。所以,你需要查看网络嗅探器来识别它: enter image description here

要做什么?如果您想检索整个页面内容,您最好在页面 javascript 中插入任何运行(评估)的库。阅读更多 here .

更新

I am not interested in retrieving the whole page and statistics or JS libraries retrieved from MediaWiki. I only need the whole content of the page (through scraping, not MediaWiki API).

问题是那些对其他资源(包括 mediawiki)的 js 调用使得将整个页面呈现给客户端成为可能。但是由于该库不支持 JS 执行,所以 js 不执行 => 页面部分没有从其他资源加载 => 目标页面不是完整的。

关于Python 请求与 urllib2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30042429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com