python ajax批量下载

#!/usr/bin/python
# !coding=utf-8

import os
import requests
import re
from urllib.parse import urlencode
from hashlib import md5
from multiprocessing.pool import Pool


def get_page(offset):
    base_url = "https://www.toutiao.com/search_content/?"
    params = {
        "offset": offset,
        "format": "json",
        "keyword": "火影劫",
        "autoload": "true",
        "count": "20",
        "cur_tab": "1",
        "from": "search_tab",
        "pd": "synthesis"
    }

    url = base_url + urlencode(params)
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
    except requests.ConnectionError:
        return None


def get_images(json):
    if json:
        for item in json.get("data"):
            if item.get("open_url") is not None:
                title = item.get("title")
                content = re.sub("[\s+\.\!\/_,::$%^*“”《》(+\"\']+|[+——!,。??、~@#¥%……&*()]+", "", title)
                images = item.get("image_url")
                yield {
                    "title": content,
                    "image": "http://" + images[2:]
                }


def save_images(item):
    if not os.path.exists(item["title"]):
        os.mkdir(item["title"])
    try:
        response = requests.get(item["image"])
        path = "{0}/{1}.{2}".format(item["title"], md5(response.content).hexdigest(), "jpg")
        with open(path, "wb") as f:
            f.write(response.content)
    except requests.ConnectionError:
        print("Have Error!")


def main():
    for offset in range(0, 2):
        json = get_page(offset * 20)
        for item in get_images(json):
            save_images(item)


GROUP_START = 1
GROUP_END = 1

if __name__ == "__main__":
    pool = Pool()
    groups = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])
    pool.map(main, groups)
    pool.close()
    pool.join()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值