Python 无法通过wget e或脚本访问url
大家好!我想通过python脚本访问一些网页。网址是: 当我通过网络浏览器访问它时,一切正常。但当我想使用urllib2访问它时:Python 无法通过wget e或脚本访问url,python,bash,web-scraping,web-crawler,Python,Bash,Web Scraping,Web Crawler,大家好!我想通过python脚本访问一些网页。网址是: 当我通过网络浏览器访问它时,一切正常。但当我想使用urllib2访问它时: a = urllib2.urlopen("http://www.idealo.de/preisvergleich/Shop/27039.html") 它给了我以下错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/
a = urllib2.urlopen("http://www.idealo.de/preisvergleich/Shop/27039.html")
它给了我以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 406, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 444, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: Forbidden
错误是:
--2012-04-23 12:42:03-- http://www.idealo.de/preisvergleich/Shop/27039.html
Resolving www.idealo.de (www.idealo.de)... 62.146.49.133
Connecting to www.idealo.de (www.idealo.de)|62.146.49.133|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2012-04-23 12:42:03 ERROR 403: Forbidden.
有人能解释为什么会这样吗?我如何使用python访问它?他们正在阻止一些用户代理。如果您尝试以下操作:
wget -U "Mozilla/5.0" http://www.idealo.de/preisvergleich/Shop/27039.html
它起作用了。因此,您必须找到在python代码中伪造用户代理的方法,以使其正常工作
试试这个:
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
a = opener.open("http://www.idealo.de/preisvergleich/Shop/27039.html")
他们正在阻止一些用户代理。如果您尝试以下操作:
wget -U "Mozilla/5.0" http://www.idealo.de/preisvergleich/Shop/27039.html
它起作用了。因此,您必须找到在python代码中伪造用户代理的方法,以使其正常工作
试试这个:
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
a = opener.open("http://www.idealo.de/preisvergleich/Shop/27039.html")
urllib2文档中的最后一个示例将非常有用urllib2文档中的最后一个示例将非常有用