我在学习beautifulsoup,并试图编写一个小脚本在荷兰房地产网站上查找房屋。当我试图让网站的内容,我立即得到一个HTTP405错误:使用urllib获取网站会导致HTTP 405错误
File "funda.py", line 2, in <module>
html = urlopen("http://www.funda.nl")
File "<folders>request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "<folders>request.py", line 532, in open
response = meth(req, response)
File "<folders>request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "<folders>request.py", line 570, in error
return self._call_chain(*args)
File "<folders>request.py", line 504, in _call_chain
result = func(*args)
File "<folders>request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 405: Not Allowed
什么我尝试执行:
from urllib.request import urlopen
html = urlopen("http://www.funda.nl")
知道为什么这是导致HTTP405?我只是在做一个GET请求,对吧?
import urllib
html = urllib.urlopen("http://www.funda.nl")
leovp的评论是有道理的:
这绝对是一个GET请求,但你被检测为一个机器人,而这个特定的服务器在这种情况下发送405错误代码。尝试将标题调整为正常浏览器。 – leovp
相关 - https://stackoverflow.com/questions/27652543/how-to-use-python-requests-to-fake-a-browser-visit?noredirect=1&lq=1 –