【Python爬虫】第五课(b站弹幕)

首先,非常感谢大神的文章 https://www.cnblogs.com/LexMoon/p/pyspider03.html#4361286

import requests
import re
av_id = '67946325'
headers = {
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    'Accept': 'text/html',
    'Cookie': "嘿嘿"

}
resp = requests.get('https://www.bilibili.com/video/av'+av_id,headers=headers)

match_rule = r'cid=(.*?)&aid'
oid = re.search(match_rule,resp.text).group().replace('cid=','').replace('&aid','')
print('oid='+oid)

xml_url = 'https://api.bilibili.com/x/v1/dm/list.so?oid='+oid

resp = requests.get(xml_url,headers=headers)

if resp.encoding == 'ISO-8859-1': encodings = requests.utils.get_encodings_from_content(resp.text) if encodings: encoding = encodings[0] else: encoding = resp.apparent_encoding global encode_content encode_content = resp.content.decode(encoding,'replace') print(encode_content) #爬虫headers需要包含什么内容才不会返回404呢?我尝试7个全写,发现就不对。 #正则表达式快忘记了…… #最后的乱码解决方案

 

转载于:https://www.cnblogs.com/break03/p/11575327.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值