首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python3.4 urllib.request错误(http 403)

Python3.4 urllib.request错误(http 403)
EN

Stack Overflow用户
提问于 2015-02-08 23:57:27
回答 1查看 26.3K关注 0票数 26

我正在尝试打开并解析一个html页面。在python 2.7.8中,我没有问题:

代码语言:javascript
复制
import urllib
url = "https://ipdb.at/ip/66.196.116.112"
html = urllib.urlopen(url).read()

一切都很好。然而,我想转到python3.4,在那里我得到了HTTP错误403 (禁止)。我的代码:

代码语言:javascript
复制
import urllib.request
html = urllib.request.urlopen(url) # same URL as before

File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 461, in open
response = meth(req, response)
File "C:\Python34\lib\urllib\request.py", line 574, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python34\lib\urllib\request.py", line 499, in error
return self._call_chain(*args)
File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 582, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

它适用于其他不使用https的URL。

代码语言:javascript
复制
url = 'http://www.stopforumspam.com/ipcheck/212.91.188.166'

没问题。

EN

回答 1

Stack Overflow用户

发布于 2016-04-11 01:18:57

以下是我在学习python-3时在urllib上收集的一些笔记:

我保存着它们,以防它们会派上用场,或者帮助别人。

如何导入urllib.requesturllib.parse

代码语言:javascript
复制
import urllib.request as urlRequest
import urllib.parse as urlParse

如何发出GET请求:

代码语言:javascript
复制
url = "http://www.example.net"
# open the url
x = urlRequest.urlopen(url)
# get the source code
sourceCode = x.read()

如何发出POST请求:

代码语言:javascript
复制
url = "https://www.example.com"
values = {"q": "python if"}
# encode values for the url
values = urlParse.urlencode(values)
# encode the values in UTF-8 format
values = values.encode("UTF-8")
# create the url
targetUrl = urlRequest.Request(url, values)
# open the url
x  = urlRequest.urlopen(targetUrl)
# get the source code
sourceCode = x.read()

如何发出POST请求(403 forbidden响应):

代码语言:javascript
复制
url = "https://www.example.com"
values = {"q": "python urllib"}
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
# encode values for the url
values = urlParse.urlencode(values)
# encode the values in UTF-8 format
values = values.encode("UTF-8")
# create the url
targetUrl = urlRequest.Request(url = url, data = values, headers = headers)
# open the url
x  = urlRequest.urlopen(targetUrl)
# get the source code
sourceCode = x.read()

如何发出GET请求(403 forbidden响应):

代码语言:javascript
复制
url = "https://www.example.com"
# pretend to be a chrome 47 browser on a windows 10 machine
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
req = urlRequest.Request(url, headers = headers)
# open the url
x = urlRequest.urlopen(req)
# get the source code
sourceCode = x.read()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28396036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档