"""
使用随机User-Agent库,
爬取笔趣阁万古天帝章节目录
"""
import requests
from fake_useragent import FakeUserAgent
import random
import chardet
def biquge():
# 定义url,
url = "https://www.biqudao.com/bqge25289/"
# 使用第三方库随机获得一个userAgent
userAgent = FakeUserAgent().random
print(userAgent)
print(type(userAgent))
# 传入字典中
headers= {
"User-Agent":userAgent
}
# 返回字节流数据
bytesHtml=requests.get(url,headers=headers).content
# 判断网页编码格式,返回一个字典
res=chardet.detect(bytesHtml)
# 解码
html=bytesHtml.decode(res["encoding"])
print(html)
biquge()
转载于:https://www.cnblogs.com/YangQingHong/p/10978251.html