【问题标题】:HTTP Error 403: Forbidden urlib2 Python 2.7HTTP 错误 403:禁止 urllib2 Python 2.7
【发布时间】:2016-06-20 10:47:49
【问题描述】:

我已经成功地能够使用 urllib2,但是对于这个网站,我突然测试它不起作用。我已经在论坛上查看并尝试了一些修复程序,但它似乎不起作用。下面是一种解决方法的示例,但对我不起作用。有人可以帮助我连接到它。

给出错误的代码:

from bs4 import BeautifulSoup
import urllib2

proxy_support = urllib2.ProxyHandler({"http":"http://username:password@ip:port"})
hdr = {'Accept': 'text/html,application/xhtml+xml,*/*'}
url = 'http://www.carnextdoor.com.au/'
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
req=urllib2.Request(url,headers=hdr)
#Here I get the error with and without using the header or going html = urllib2.urlopen(url).read()
html = urllib2.urlopen(req).read()
soup=BeautifulSoup(html,"html5lib")
print soup

【问题讨论】:

  • 你有可能被屏蔽了
  • 按回答是网站

标签: python html http urllib2


【解决方案1】:

在添加用户代理之前,我得到了 403,以下对我来说已经足够了:

hdr = {'Accept': 'text/html,application/xhtml+xml,*/*',"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"}
url = 'http://www.carnextdoor.com.au/'


req=urllib2.Request(url,headers=hdr)
#Here I get the error with and without using the header or going html = urllib2.urlopen(url).read()
html = urllib2.urlopen(req).read()
soup=BeautifulSoup(html,"html5lib")
print soup

没有用户代理:

In [10]: hdr = {'Accept': 'text/html,application/xhtml+xml,*/*'}

In [11]: url = 'http://www.carnextdoor.com.au/'

In [12]: req=urllib2.Request(url,headers=hdr)

In [13]: html = urllib2.urlopen(req).read()
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
<ipython-input-13-dbeb64d95cd3> in <module>()
----> 1 html = urllib2.urlopen(req).read()

使用用户代理:

In [20]: hdr = {'Accept': 'text/html,application/xhtml+xml,*/*',"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"}

In [21]: req=urllib2.Request(url,headers=hdr)
In [22]: html = urllib2.urlopen(req).read()
In [23]: 

在没有任何用户代理的情况下使用requests 也可以正常工作。

In [28]: import requests

In [29]: r = requests.get(url)

In [30]: r.status_code
Out[30]: 200

【讨论】:

  • 哇,我尝试了除用户代理之外的所有标头。谢谢你。愚蠢的错误:(
  • 别担心,这通常是我要尝试的事情清单上的第一个。
猜你喜欢
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2011-07-12
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多