工作中有个简单的爬虫:爬取一个api 得到列表数据,再爬取每个元素api。
展示代码不涉及机密信息,兼容py2,py3,
我写的第一个版本是各种for循环,可以完成业务的需求。为了降低耦合性,不同逻辑拆开,空间上使用了列表和队列,
用到了递归,多线程。
使用多线程,时间缩短了一半
# -*- coding: utf-8 -*-
import os
import json
import sys
import requests
import threading
import time
# from concurrent.futures import ThreadPoolExecutor # Python2 需要安装使用
from multiprocessing.pool import ThreadPool
try:
from queue import Queue
except ImportError:
from Queue import Queue
class Main(object):
def __init__(self, date, bucket, purpose):
self.url = "http://10.146.100.40:8880/pf/xteam/collect/get_first_data"
self.detail_url = "http://10.146.100.40:8880/pf/xteam/collect/get_detail_data"
self.group_queue = []
self.download_link_queue = Queue()
self.bucket = bucket
self.purpose = purpose
self.date &#