码迷,mamicode.com
首页 > Web开发 > 详细

urllib.error.HTTPError: HTTP Error 403: Forbidden

时间:2020-05-07 15:38:29      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:request   utf-8   表格   urlencode   response   data   from   pos   pen   

# 简单例子
import urllib.request

request = urllib.request.Request(‘https://python.org‘)
response = urllib.request.urlopen(request)
# print(response.read().decode(‘utf-8‘))

# 增加header
from urllib import request, parse

# url = ‘http://httpbin.org/post‘
url = ‘https://upload.wikimedia.org/wikipedia/commons/e/e0/Caravaggio_-_Cena_in_Emmaus.jpg‘
headers = {
    ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0‘
}
# 构造POST表格
dict = {
    ‘name‘: ‘Germey‘
}
data = bytes(parse.urlencode(dict), encoding=‘utf8‘)
req = request.Request(url=url, data=data, headers=headers, method=‘GET‘)
response = request.urlopen(req)

print(response)

urllib.error.HTTPError: HTTP Error 403: Forbidden

标签:request   utf-8   表格   urlencode   response   data   from   pos   pen   

原文地址:https://www.cnblogs.com/zhenzi0322/p/12843078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!