dianshiju.py

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值