无法使用python获取网站urllib.urlopen()或Shi以外的任何web浏览器

2024-05-14 06:15:18 发布

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

这是我要获取的网站的URL

https://salami.parc.com/spartag/GetRepository?friend=jmankoff&keywords=antibiotic&option=jmankoff%27s+tags

当我用以下代码获取网站并用以下代码显示内容时:

sock = urllib.urlopen("https://salami.parc.com/spartag/GetRepository?friend=jmankoff&keywords=antibiotic&option=jmankoff's+tags")
html = sock.read()
sock.close()
soup = BeautifulSoup(html)
print soup.prettify()

我得到以下输出:

^{pr2}$

对于urllib2,我也得到了相同的结果。有趣的是,这个URL只能在Shiretoko web浏览器v3.5.7上运行。(当我说它有效时,我的意思是它给我带来了正确的页面)。当我把这个网址输入到firefox3.0.15或konquerorv4.2.2时。我得到了完全相同的错误页面(带有“无效输入数据”)。我不知道是什么造成了这种差异,也不知道如何使用Python获取这个页面。有什么想法吗?在

谢谢


Tags: httpsfriendcomurl网站页面sockoption
2条回答

如果你看到urllib2文档,它会说

urllib2.build_opener([handler, ...])¶

    .....
    If the Python installation has SSL support (i.e., if the ssl module can be imported), HTTPSHandler will also be added. 

    .....

您可以尝试将urllib2与ssl模块一起使用。或者,您可以使用httplib

这正是你用网络浏览器点击链接时得到的结果。也许你应该登录或者准备一个饼干之类的

对于linux上的Firefox3.5.8(shiretoko),我也收到了同样的消息

相关问题 更多 >