import requests
import csv
class MySpider:
types_list = ['演出','展览','本地生活']
def __init__(self,header={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42'}):
self.header = header
@staticmethod
def get_city():
city_data = requests.get('https://show.bilibili.com/api/ticket/city/list?channel=4').json()['data']
city_list = [x['list'] for x in city_data['more']]
city_list.append(city_data['list'])
city_id_list = [city_data['id'] for city_id_list in city_list for city_data in city_id_list ]
city_id_list = [110100]
return city_id_list
def save_file(self,area,data):
op = open('{}.txt'.format(area),'a+')
op.write(str(data['project_id'])+','+data['project_name']+'\n')
op.close()
def save_csv(self,area,data):
with open('{}.csv'.format(area), 'a+',encoding='gbk') as f:
csv_writer = csv.writer(f)
list1 = [str(data['project_id']),data['project_name']]
csv_writer.writerow(list1)
def get_info(self,area,every_page_url):
print(every_page_url)
result = requests.get(every_page_url).json()['data']['result']
if len(result)==0:
return
else:
for i in result:
self.save_file(area, {'project_name':i['project_name'],'project_id':i['project_id']})
self.save_csv(area, {'project_name': i['project_name'], 'project_id': i['project_id']})
return 1
def get_every_info(self,page=1):
city_id_list = self.get_city()
for types in self.types_list:
for area in city_id_list:
while True:
every_page_url = 'https://show.bilibili.com/api/ticket/project/listV2?version=134&page={}&pagesize=16&area={}&filter=&platform=web&p_type={}'.format(page,area,types)
data = self.get_info(area,every_page_url)
if not data:
break
else:
page+=1
a = MySpider()
print(a.get_every_info())
bili会员购
最新推荐文章于 2024-09-02 23:14:47 发布