马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:import urllib.request
import chardet
def main():
url = 'https://www.bilibili.com'
head = {}
head["user-agent"] = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36"
head['authority'] = 's.search.bilibili.com'
head['method'] = 'GET'
head['path'] = '/main/suggest?func=suggest&suggest_type=accurate&sub_type=tag&main_ver=v1&highlight=&userid=5146541&bangumi_acc_num=1&special_acc_num=1&topic_acc_num=1&upuser_acc_num=3&tag_num=10&special_num=10&bangumi_num=10&upuser_num=3&term=%E4%B8%BA%E4%BB%80%E4%B9%88B%E7%AB%99%E8%AE%BE%E7%BD%AE1%E4%BA%86headers%E8%BF%98%E6%98%AF%E4%B8%8D%E8%83%BD%E7%88%AC%E5%8F%96&rnd=0.08535939713748864'
head['scheme'] = 'https'
head['accept'] = 'application/json, text/plain, */*'
head['accept-encoding'] = 'gzip, deflate, br'
head['accept-language'] = 'zh-CN,zh;q=0.9'
head['origin'] = 'https://www.bilibili.com'
head['referer'] = 'https://www.bilibili.com/'
head['sec-fetch-dest'] = 'empty'
head['sec-fetch-mode'] = 'cors'
head['sec-fetch-site'] = 'same-site'
req = urllib.request.Request(url,headers = head)
response = urllib.request.urlopen(url)
html = response.read()
# 识别网页编码
encode = chardet.detect(html)['encoding']
if encode == 'GB2312':
encode = 'GBK'
print("该网页使用的编码是:%s" % encode)
print(req.headers)
if __name__ == "__main__":
main()
response = urllib.request.urlopen(req)
|