python协程池爬虫,python多协程爬虫示例

from gevent import monkey

monkey.patch_all()

#打上多协程布丁,下面的程序就可以执行多协程了

import requests,gevent,csv

from gevent.queue import Queue

from bs4 import BeautifulSoup

#把所有URL都放到一个列表里:

url_list=[]

i=1

for i in range(10):

i=i+1

url='http://www.mtime.com/top/tv/top100/index-'+str(i)+'.html'

url_list.append(url)

#第一个url和别的不一样,需要单独加入

url_0='http://www.mtime.com/top/tv/top100/'

url_list.append(url_0)

headers={

'User-Agent':

}

csv_file=open('时光网电影列表.csv','a+',newline='',encoding='utf-8')

writer=csv.writer(csv_file)

file_head=['电影名称','导演','主演','简介']

writer.writerow(file_head)

def list(movies):

for movie in movies:

title=movie.find('h2',class_="px14 pb6").find('a').text

acts=movie.find_all('p')

try:

dic=acts[0].text

except IndexError:

dic='none'

try:

actor=acts[1].text

except IndexError:

actor='none'

try:

bief=movie.find('p',class_="mt3").text

except AttributeError:

bief='none'

writer.writerow([title,dic,actor,bief])

#所有url都放到‘不用等’房间里:

work=Queue()

for url in url_list:

work.put_nowait(url)

#爬虫对象:

def crawler():

while not work.empty():

url=work.get_nowait()

res=requests.get(url,headers=headers)

soup=BeautifulSoup(res.text,'html.parser')

movies=soup.find_all('div',class_="mov_con")

list(movies)

print(url,work.qsize(),res.status_code)

#建立多协程任务,任务不用建太多,2个就够,太多的话对方服务器承受不了

tasks_list=[]

for x in range(2):

task=gevent.spawn(crawler)

tasks_list.append(task)

gevent.joinall(tasks_list)

csv_file.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值