真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

多進(jìn)程爬取

import requests
from lxml import etree
import re
import time
from multiprocessing import Pool  #導(dǎo)入multiprocessing庫的Pool模塊

headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36'}

def get_info(url):
    html = requests.get(url,headers = headers)
    selector = etree.HTML(html.text)
    names = selector.xpath('//*[@class="article block untagged mb15 typs_hot"]/div[1]/a[2]/h3/text()')
    centents = re.findall('
.*?(.*?)',html.text,re.S) #第一個正則是為了匹配換行符 laughs = re.findall('(\d+)',html.text,re.S) comments = re.findall('(\d+) 評論',html.text,re.S) for name,centent,laugh,comment in zip(names,centents,laughs,comments): info = { 'name':name, 'centent':centents, 'laugh':laughs, 'comment':comments } return (info) if __name__ == '__main__': urls = ["https://www.qiushibaike.com/text/page/{}/".format(num)for num in range(0,14)] start_1 = time.time() for url in urls: get_info(url) end_1 = time.time() print('串行爬取花費(fèi)時間:' + str(end_1 - start_1)) start_2 = time.time() pool = Pool(processes=2) #創(chuàng)建進(jìn)程池,processes為設(shè)置的進(jìn)程個數(shù) pool.map(get_info,urls) #利用map()函數(shù)運(yùn)行進(jìn)程,參數(shù)fuc為運(yùn)行的函數(shù),iterable為迭代參數(shù) end_2 = time.time() print('2個進(jìn)程:' + str(end_2 - start_2)) start_3 = time.time() pool = Pool(processes=4) # 創(chuàng)建進(jìn)程池,processes為設(shè)置的進(jìn)程個數(shù) pool.map(get_info, urls) # 利用map()函數(shù)運(yùn)行進(jìn)程,參數(shù)fuc為運(yùn)行的函數(shù),iterable為迭代參數(shù) end_3 = time.time() print('4個進(jìn)程:' + str(end_3 - start_3))

輸出:

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供尼河口網(wǎng)站建設(shè)、尼河口做網(wǎng)站、尼河口網(wǎng)站設(shè)計(jì)、尼河口網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、尼河口企業(yè)網(wǎng)站模板建站服務(wù),10余年尼河口做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

D:\Python\venv\Scripts\python.exe D:/Python/venv/test12.py
串行爬取花費(fèi)時間:5.043288469314575
2個進(jìn)程:3.351191759109497
4個進(jìn)程:2.882164716720581

Process finished with exit code 0

新聞標(biāo)題:多進(jìn)程爬取
文章轉(zhuǎn)載:http://weahome.cn/article/jdgscj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部