import requests import json # https://m.douban.com/rexxar/api/v2/subject_collection/filter_tv_english_hot/items?os=android&for_mobile=1&callback=jsonp1&start=0&count=18&loc_id=108288&_=0 # https://m.douban.com/rexxar/api/v2/subject_collection/filter_tv_english_hot/items?os=android&for_mobile=1&callback=jsonp1&start=0&count=18&loc_id=108288&_=0 class TV: def __init__(self,name): self.name = name self.url = 'https://m.douban.com/rexxar/api/v2/subject_collection/filter_tv_english_hot/items?start={}&count=18&loc_id=108288' self.headers = { 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36' 'Referer':'https:// m.douban.com / tv /' + self.name } def get_url_list(self): rep = [self.url.format(i * 18) for i in range(500)] return rep def parse_url(self,url): resp = requests.get(url,headers = self.headers) json_str = resp.content.decode() dicy = json.loads(json_str) return dicy def save_html(self,html_str,url_page): file_path = '{}第{}页.html'.format(self.name,url_page) with open(file_path,'w',encoding='utf-8') as f: f.write(json.dumps(html_str,ensure_ascii=False,indent=2)) def run(self): url_list = self.get_url_list() for url in url_list: url_page = url_list.index(url) + 1 html_str = self.parse_url(url) pp = html_str['total'] self.save_html(html_str,url_page) if __name__ == '__main__': print('输入你想看电视的类型') print("1.美剧, 2.英剧") a = int(input(":")) if a == 1: aa = TV('american') else: aa = TV('british') aa.run()