爬虫之 --爬取豆瓣电影

## 话不多说,直接贴码, 适合小白

# conding=utf-8
import json

import requests


class Douban():
    def __init__(self):
        self.url_temp = (
        "https://m.douban.com/rexxar/api/v2/subject_collection/tv_american/items?start={}&count=18&loc_id=108288")
        self.headers = {
            "Referer": "https://m.douban.com/tv/american",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A Safari/604.1"
        }

    def parse_url(self, url):
        print(url)
        resp = requests.get(url, headers=self.headers)
        json_str = resp.content.decode()
        return json_str

    def get_content_list(self, json_str):
        temp_dict = json.loads(json_str)
        return temp_dict["subject_collection_items"]

    def save_content_list(self, content_list):
        with open('douban.txt', "w", encoding="utf-8") as f:
            for content in content_list:
                f.write(json.dumps(content, ensure_ascii=False))
                f.write("\n")
        print("保存成功")

    def run(self):
        num = 0
        while True:
            # 构造 start_url
            next_url = self.url_temp.format(num)
            # 获取数据,返回响应
            json_str = self.parse_url(next_url)
            # 提取数据
            content_list = self.get_content_list(json_str)
            # 保存
            self.save_content_list(content_list)
            # 构造下一页的url地址, 循环2-5
            num += 18

            if len(content_list) < 18:
                break


if __name__ == '__main__':
    douban = Douban()
    douban.run()





 

你可能感兴趣的:(爬虫)