鱼C论坛

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

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

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

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

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

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()

E5${E@C}KKK6}X[OQ~`]Y0M.png
最佳答案
2020-8-5 22:10:11
response = urllib.request.urlopen(req)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-5 22:01:52 | 显示全部楼层
只添加"user-agent"也无效
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-5 22:10:11 | 显示全部楼层    本楼为最佳答案   
response = urllib.request.urlopen(req)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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



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

改成这样:
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(req)
    html = response.read()
    # 识别网页编码
    encode = chardet.detect(html)['encoding']
    if encode == 'GB2312':
        encode = 'GBK'

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


if __name__ == "__main__":
    main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢,已解决
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-19 17:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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