直接上代码,解释在注释,不懂评论留言,必回
# 项目名称:
# 项目简介:
# 作 者:key
# 开发时间:2020/10/24 19:22
from urllib.request import urlopen
from urllib.request import Request
from fake_useragent import UserAgent
from random import choice
url = "http://www.baidu.com"
# 获取请求头,在network中,或者fiddler中抓取
# 创建动态请求头,避免自己每次都手动切换user-agent
# 方法一,创建user-agent列表,拷贝一些不同浏览器的user-agent,然后用random.choice(user-agent)去随机获取
# useragent = [
# 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36',
# 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
# 'Mozilla/5.0 (Windows ME 4.9; rv:31.0) Gecko/20100101 Firefox/31.7',
# 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; chromeframe/13.0.782.215)'
# ]
# # print(choice(useragent))
# headers = {
# 'User-Agent':choice(useragent)
# }
# # 方法二,导入第三方包,fake-useragent,然后条用UserAgent类对象
# ua = UserAgent()
# # print(ua.chrome)
# # print(ua.ie)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/84.0.4147.89 Safari/537.36 "
}
# 包装成浏览器访问
request = Request(url, headers=headers)
#打印返回请求头
#这里有一个坑,就是在headers字典中,给User-Agent赋值的时候,底层会自动将User的首字母大写
#get_header返回请求头的时候,Agent首字母大写,将获取不到数据(我也不知道为什么,反正就是这样)
#print(request.get_header('User-agent')) # 返回Mozilla/5.0 ...... Chrome/84.0.4147.89 Safari/537.36
#print(request.get_header('User-Agent')) # 返回None
# 进行访问
response = urlopen(request)
#返回读取数据
info = response.read()
# 转化成unicode编码格式
print(info.decode())