1. 同步爬取时光网Top100
利用requests和bs4模块就行了
import requests
from bs4 import BeautifulSoup
import time
start=time.time()
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
#获取时光网TOP100的十个网址
url_1='http://www.mtime.com/top/movie/top100/'
list_url=[] #将需要的网址封装成列表
list_url.append(url_1)
for i in range(2,11):
url='http://www.mtime.com/top/movie/top100/index-'+str(i)+'.html'
list_url.append(url)
for i in list_url: #遍历网址
res=requests.get(i,headers=headers) #发出请求
bs=BeautifulSoup(res.text,'html.parser') #解析数据
tagall=bs.find_all('div',class_="mov_con")
for tag in tagall:
title=tag.find('h2').find('a').text #爬取电影名
lj=tag.find('h2').find('a')['href'] #爬取电影的网址
print(title,lj)
end=time.time() #结束时间
print(f"共用时:{end-start:.2f}秒") #共耗时等于结束时间减去开始时间
2. 利用gevent库异步爬取时光网Top100
安装模块 pip install gevent
#在导入其他库和模块前,先把monkey模块导入进来,并运行monkey.patch_all()。这样,才能先给程序打上补丁。
from gevent import monkey #从gevent库里导入了monkey模块,这个模块能将程序转换成可异步的程序
monkey.patch_all() #它的作用其实就像你的电脑有时会弹出“是否要用补丁修补漏洞或更新”一样。它能给程序打上补丁,让程序变成是异步模式,而不是同步模式。它也叫“猴子补丁”。
import gevent,time,requests #我们导入了gevent库来帮我们实现多协程,导入了time模块来帮我们记录爬取所需时间,导入了requests模块帮我们实现爬取10个网站。
from bs4 import BeautifulSoup
import time
start=time.time() #开始时间
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
#获取时光网TOP100的十个网址
url_1='http://www.mtime.com/top/movie/top100/'
list_url=[] #将需要的网址封装成列表
list_url.append(url_1)
for i in range(2,11):
url='http://www.mtime.com/top/movie/top100/index-'+str(i)+'.html'
list_url.append(url)
def cra(url): #定义一个函数,用来执行解析网址和爬取内容
res=requests.get(url,headers=headers) #发出请求
bs=BeautifulSoup(res.text,'html.parser') #解析数据
tagall=bs.find_all('div',class_="mov_con")
for tag in tagall:
title=tag.find('h2').find('a').text #爬取电影名
lj=tag.find('h2').find('a')['href'] #爬取电影的网址
print(title,lj)
tasks_list = [] #创建空列表
for url in list_url: #遍历网址
task = gevent.spawn(cra,url) #用gevent.spawn()创建任务,此任务可以调用cra(url)函数,注意括号里参数的写法
tasks_list.append(task) #将任务加入列表
gevent.joinall(tasks_list) #调用gevent库里的joinall方法,能启动执行所有的任务。gevent.joinall(tasks_list)就是执行tasks_list这个任务列表里的所有任务,开始爬取。
end = time.time() #结束时间
print(f"共用时:{end-start:.2f}秒") #共耗时
3. 结合gevent库中的queue模块创建多个爬虫异步爬取时光网Top100
#在导入其他库和模块前,先把monkey模块导入进来,并运行monkey.patch_all()。这样,才能先给程序打上补丁。
from gevent import monkey #从gevent库里导入了monkey模块,这个模块能将程序转换成可异步的程序
monkey.patch_all() #它的作用其实就像你的电脑有时会弹出“是否要用补丁修补漏洞或更新”一样。它能给程序打上补丁,让程序变成是异步模式,而不是同步模式。它也叫“猴子补丁”。
import gevent,time,requests #我们导入了gevent库来帮我们实现多协程,导入了time模块来帮我们记录爬取所需时间,导入了requests模块帮我们实现爬取10个网站。
from bs4 import BeautifulSoup
from gevent.queue import Queue #从gevent库里导入queue模块
start=time.time() #开始时间
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
#获取时光网TOP100的十个网址
url_1='http://www.mtime.com/top/movie/top100/'
list_url=[] #将需要的网址封装成列表
list_url.append(url_1)
for i in range(2,11):
url='http://www.mtime.com/top/movie/top100/index-'+str(i)+'.html'
list_url.append(url)
work=Queue() #创建队列对象,并赋值给work。
for i in list_url: #遍历网址
work.put_nowait(i) #将网址放进队列里面
def cra(): #定义一个函数,用来执行解析网址和爬取内容
while not work.empty(): #当队列不是空的时候,就执行下面的程序。
url = work.get_nowait() #将网址从队列里面提取出来
res=requests.get(url,headers=headers) #发出请求
bs=BeautifulSoup(res.text,'html.parser') #解析数据
tagall=bs.find_all('div',class_="mov_con")
for tag in tagall:
title=tag.find('h2').find('a').text #爬取电影名
lj=tag.find('h2').find('a')['href'] #爬取电影的网址
print(title,lj)
tasks_list = [] #创建空列表
for i in range(2): #创建2个任务,相当于2条爬虫
task = gevent.spawn(cra) #用gevent.spawn()创建任务,此任务可以调用cra函数,注意括号里参数的写法
tasks_list.append(task) #将任务加入列表
gevent.joinall(tasks_list) #调用gevent库里的joinall方法,能启动执行所有的任务。gevent.joinall(tasks_list)就是执行tasks_list这个任务列表里的所有任务,开始爬取。
end = time.time() #结束时间
print(f"共用时:{end-start:.2f}秒") #共耗时