python多进程爬虫

import re
import time
from multiprocessing import Pool
import requests

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:61.0) Gecko/20100101 Firefox/61.0'
}

def re_scraper(url):
    res = requests.get(url,headers=headers)
    names = re.findall('

(.*?)

',res.text,re.S) contents = re.findall('
.*?.*?(\d+)',res.text,re.S) comments = re.findall('(\d+) 评论',res.text,re.S) infos = list() for name,content,laugh,comment in zip(names,contents,laughs,comments): info = { 'name':name, 'content':comment, 'laugh':laugh, 'comment':comment, } infos.append(info) return infos if __name__ == "__main__": # re_scraper("https://www.qiushibaike.com/8hr/page/1/") urls = ["https://www.qiushibaike.com/8hr/page/{}/".format(str(i)) for i in range(1,35)] start_1 = time.time() for url in urls: re_scraper(url) end_1 = time.time() print('串行爬虫时间:',end_1-start_1) start_2 = time.time() pool = Pool(processes=2) pool.map(re_scraper,urls) end_2 = time.time() print('2进程爬虫耗时:',end_2-start_2) start_3 = time.time() pool = Pool(processes=4) pool.map(re_scraper,urls) end_3 = time.time() print('2进程爬虫耗时:',end_3-start_3)

python多进程爬虫_第1张图片

你可能感兴趣的:(Python)