urllib库使用方法 4 create headers

import urllib.request
import urllib.parse

url = "https://www.baidu.com/"
#普通请求方法
response = urllib.request.urlopen(url)
print(response.read().decode())

#伪装头部请求方法
#构建伪装头
headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"
}
#构建请求对像
request = urllib.request.Request(url, headers = headers)
#发送请求
response = urllib.request.urlopen(request)
print(response.read().decode())

posted on 2019-02-16 15:48  PerfectData  阅读(181)  评论(0编辑  收藏  举报

导航