无法向 SO 发出 http get 请求
Unable to make a http get request to SO
我在python中有一段代码可以发出http get请求。
我可以像 http://google.com and download their page. But I can't make a get request to http://whosebug.com 一样成功地向 URL 发出请求。它显示 HTTP 403 forbidden ERROR。
但是我可以从我的浏览器访问 Whosebug。那么这个错误的原因可能是什么?
代码:
导入 urllib2
c=urllib2.urlopen('https://whosebug.com/')
内容=c.read()
打印内容[0:50]
错误:
HTTPError:HTTP 错误 403:禁止访问
这里也一样,我用的是Python3.
urllib.request.urlopen('http://whosebug.com')
失败,出现 HTTP 错误 403。
我更改了 User-Agent,然后它起作用了:
import urllib.request
urllib.request.urlopen(urllib.request.Request('http://whosebug.com/',headers={'User-Agent':'Mozilla/5.0'}))
看来 whosebug.com 会根据 User-Agent 过滤请求,而 google.com 不会这样做。
urllib2‘s default user agent string is "Python-urllib/2.6" (on Python
2.6)
我在python中有一段代码可以发出http get请求。 我可以像 http://google.com and download their page. But I can't make a get request to http://whosebug.com 一样成功地向 URL 发出请求。它显示 HTTP 403 forbidden ERROR。 但是我可以从我的浏览器访问 Whosebug。那么这个错误的原因可能是什么?
代码:
导入 urllib2
c=urllib2.urlopen('https://whosebug.com/')
内容=c.read()
打印内容[0:50]
错误: HTTPError:HTTP 错误 403:禁止访问
这里也一样,我用的是Python3.
urllib.request.urlopen('http://whosebug.com')
失败,出现 HTTP 错误 403。
我更改了 User-Agent,然后它起作用了:
import urllib.request
urllib.request.urlopen(urllib.request.Request('http://whosebug.com/',headers={'User-Agent':'Mozilla/5.0'}))
看来 whosebug.com 会根据 User-Agent 过滤请求,而 google.com 不会这样做。
urllib2‘s default user agent string is "Python-urllib/2.6" (on Python 2.6)