无法使用python urllib.urlopen()或除Shiretoko外的任何浏览器访问网站

0 投票
2 回答
860 浏览
提问于 2025-04-15 20:17

这是我想要获取的网站链接

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()

我得到了以下输出:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>
   Error message
  </title>
 </head>
 <body>
  <h2>
   Invalid input data
  </h2>
 </body>
</html>

我用urllib2也得到了相同的结果。有趣的是,这个链接只在Shiretoko浏览器v3.5.7上能正常工作。(我说正常工作是指它能带我到正确的页面)。但是当我把这个链接放到Firefox 3.0.15或Konqueror v4.2.2中时,我得到的却是完全相同的错误页面(显示“无效的输入数据”)。我不知道是什么造成了这种差异,也不知道如何用Python获取这个页面。有什么想法吗?

谢谢

2 个回答

0

当你用网页浏览器点击那个链接时,得到的就是这个结果。可能你需要先登录,或者需要设置一个什么叫“cookie”的东西。

我在Linux上用Firefox 3.5.8(也叫shiretoko)时也遇到了同样的信息。

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

撰写回答