鱼C论坛

 找回密码
 立即注册
查看: 1489|回复: 4

[已解决]为什么修改了header还是无法访问B站?

[复制链接]
发表于 2020-8-5 22:01:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下:
  1. import urllib.request
  2. import chardet

  3. def main():
  4.     url = 'https://www.bilibili.com'

  5.     head = {}
  6.     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"
  7.     head['authority'] = 's.search.bilibili.com'
  8.     head['method'] = 'GET'
  9.     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'

  10.     head['scheme'] = 'https'
  11.     head['accept'] = 'application/json, text/plain, */*'
  12.     head['accept-encoding'] = 'gzip, deflate, br'
  13.     head['accept-language'] = 'zh-CN,zh;q=0.9'
  14.     head['origin'] = 'https://www.bilibili.com'
  15.     head['referer'] = 'https://www.bilibili.com/'
  16.     head['sec-fetch-dest'] = 'empty'
  17.     head['sec-fetch-mode'] = 'cors'
  18.     head['sec-fetch-site'] = 'same-site'
  19.    
  20.    
  21.     req = urllib.request.Request(url,headers = head)
  22.     response = urllib.request.urlopen(url)
  23.     html = response.read()
  24.     # 识别网页编码
  25.     encode = chardet.detect(html)['encoding']
  26.     if encode == 'GB2312':
  27.         encode = 'GBK'

  28.     print("该网页使用的编码是:%s" % encode)
  29.     print(req.headers)
  30.         
  31. if __name__ == "__main__":
  32.     main()
复制代码


E5${E@C}KKK6}X[OQ~`]Y0M.png
最佳答案
2020-8-5 22:10:11
response = urllib.request.urlopen(req)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-5 22:01:52 | 显示全部楼层
只添加"user-agent"也无效
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-5 22:10:11 | 显示全部楼层    本楼为最佳答案   
response = urllib.request.urlopen(req)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-5 22:11:21 | 显示全部楼层
入土 发表于 2020-8-5 22:01
只添加"user-agent"也无效



你 Request 获取时候,还需要通过 urlopen 来打开 Request 的结果

改成这样:

  1. import urllib.request
  2. import chardet


  3. def main():
  4.     url = 'https://www.bilibili.com'

  5.     head = {}
  6.     head[
  7.         "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"
  8.     head['authority'] = 's.search.bilibili.com'
  9.     head['method'] = 'GET'
  10.     head[
  11.         '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'

  12.     head['scheme'] = 'https'
  13.     head['accept'] = 'application/json, text/plain, */*'
  14.     head['accept-encoding'] = 'gzip, deflate, br'
  15.     head['accept-language'] = 'zh-CN,zh;q=0.9'
  16.     head['origin'] = 'https://www.bilibili.com'
  17.     head['referer'] = 'https://www.bilibili.com/'
  18.     head['sec-fetch-dest'] = 'empty'
  19.     head['sec-fetch-mode'] = 'cors'
  20.     head['sec-fetch-site'] = 'same-site'

  21.     req = urllib.request.Request(url, headers=head)
  22.     response = urllib.request.urlopen(req)
  23.     html = response.read()
  24.     # 识别网页编码
  25.     encode = chardet.detect(html)['encoding']
  26.     if encode == 'GB2312':
  27.         encode = 'GBK'

  28.     print("该网页使用的编码是:%s" % encode)
  29.     print(req.headers)


  30. if __name__ == "__main__":
  31.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-5 22:11:58 | 显示全部楼层

谢谢,已解决
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-7 21:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表