Python爬虫正则表达爬取b站所有动漫的评分
作者:互联网
import requests import re import csv wq=1 while(wq<163): dat={ "season_version":"-1", "spoken_language_type":"-1", "area":"-1", "is_finish":"-1", "copyright":"-1", "season_status":"-1", "season_month":"-1", "year":"-1", "style_id":"-1", "order":"4", "st":"1", "sort":"0", "page":f"{wq}", "season_type":"1", "pagesize":"20", "type":"1" } header={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36" } f=open("pingfen.csv",mode="a",encoding="utf-8",newline='') csvwriter=csv.writer(f) url="https://api.bilibili.com/pgc/season/index/result/#" resp=requests.get(url,params=dat,headers=header) print(resp.text) obj=re.compile(r'"cover":"(?P<photo>.*?)","index_show":"(?P<jishu>.*?)","is_finish.*?ink":"(?P<link>.*?)","media_id.*?order":"(?P<order>.*?)","order_type".*?"title":"(?P<name>.*?)","title_icon"',re.S) result=obj.finditer(resp.text) wq=wq+1 for i in result: # print(i.group("photo")) # print(i.group("jishu")) # print(i.group("link")) # print(i.group("order")) # print(i.group("name")) dic = i.groupdict() csvwriter.writerow(dic.values()) f.close() print("over") # with open("mybaidu.txt",mode="w",encoding="utf-8") as f: # f.write(resp.text) # print("over")
正则表达相关参数:
爬取结果存入文件下
包含动漫名,图片,链接,综合评分,动画名
网上找的b站数据爬取基本都用不了,爬取不了b站综合评分,就自己学习写了个正则表达,毕设都整完了,这个功能就没加进去,还是时间不够没法学习
标签:group,Python,爬虫,爬取,wq,print,import,order 来源: https://www.cnblogs.com/520520520zl/p/16171581.html