Python爬虫系统化学习(5)
Python爬虫系统化学习(5)
多线程爬虫,在之前的网络编程中,我学习过多线程socket进行单服务器对多客户端的连接,通过使用多线程编程,可以大大提升爬虫的效率。
Python多线程爬虫主要由三部分组成:线程的创建,线程的定义,线程中函数的调用。
线程的创建:多通过for循环调用进行,通过thread.start()唤醒线程,thread.join()等待线程自动阻塞
示例代码如下:
for i in range(1,6):
thread=MyThread("thread"+str(i),list[i-1])
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
线程的定义:线程的定义使用了继承,通常定义线程中包含两个函数,一个是init初始化函数,在类创建时自动调用,另一个是run函数,在thread.start()函数执行时自动调用,示例代码如下:
class MyThread(threading.Thread):
def __init__(self,name,link_s):
threading.Thread.__init__(self)
self.name=name
def run(self):
print('%s is in Process:'%self.name)
#通过spider我们调用了爬虫函数
spider(self.name,self.links)
print('%s is out Process'%self.name)
线程中函数的调用是在run里面进行的,而多线程爬虫的重点就是将多线程与爬虫函数紧密结合起来,这就需要我们为爬虫们分布任务,也就是每个函数都要爬些什么内容。
首先我编写了个写文件,将贝壳找房的1-300页南京租房网址链接写入a.txt,代码如下:
zurl="https://nj.zu.ke.com/zufang/pg"
for i in range(101,300):
turl=url+str(i)+'\n'
print(turl)
with open ('a.txt','a+') as f:
f.write(turl)
其次在main函数中将这些链接写入元组中
link_list=[]
with open('a.txt',"r") as f:
file_list=f.readlines()
for i in file_list:
i=re.sub('\n','',i)
link_list.append(i)
此后通过调用link_list[i]就可以为每个爬虫布置不同的任务了
max=len(link_list) #max为最大页数
page=0 #page为当前页数
def spider(threadName, link_range):
global page
global max
while page<=max:
i = page
page+=1
try:
r = requests.get(link_list[i], timeout=20)
soup = BeautifulSoup(r.content, "lxml")
house_list = soup.find_all("div", class_="content__list--item")
for house in house_list:
global num
num += 1
house_name = house.find('a', class_="twoline").text.strip()
house_price = house.find('span', class_="content__list--item-price").text.strip()
info ="page:"+str(i)+"num:" + str(num) + threadName + house_name + house_price
print(info)
except Exception as e:
print(threadName, "Error", e)
如此这些线程就可以异步的进行信息获取了,整体代码如下
#coding=utf-8
import re
import requests
import threading
import time
from bs4 import BeautifulSoup
page=0
num=0
link_list=[]
with open('a.txt',"r") as f:
file_list=f.readlines()
for i in file_list:
i=re.sub('\n','',i)
link_list.append(i)
max=len(link_list)
print(max)
class MyThread(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name=name
def run(self):
print('%s is in Process:'%self.name)
spider(self.name)
print('%s is out Process'%self.name)
max=len(link_list) #max为最大页数
page=0 #page为当前页数
def spider(threadName):
global page
global max
while page<=max:
i = page
page+=1
try:
r = requests.get(link_list[i], timeout=20)
soup = BeautifulSoup(r.content, "lxml")
house_list = soup.find_all("div", class_="content__list--item")
for house in house_list:
global num
num += 1
house_name = house.find('a', class_="twoline").text.strip()
house_price = house.find('span', class_="content__list--item-price").text.strip()
info ="page:"+str(i)+"num:" + str(num) + threadName + house_name + house_price
print(info)
except Exception as e:
print(threadName, "Error", e)
start = time.time()
for i in range(1,6):
thread=MyThread("thread"+str(i))
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
end=time.time()
print("All using time:",end-start)
此外多线程爬虫还可以与队列方式结合起来,产生全速爬虫,速度会更快一点:具体完全代码如下:
#coding:utf-8
import threading
import time
import re
import requests
import queue as Queue
link_list=[]
with open('a.txt','r') as f:
file_list=f.readlines()
for each in file_list:
each=re.sub('\n','',each)
link_list.append(each)
class MyThread(threading.Thread):
def __init__(self,name,q):
threading.Thread.__init__(self)
self.name=name
self.q=q
def run(self):
print("%s is start "%self.name)
crawel(self.name,self.q)
print("%s is end "%self.name)
def crawel(threadname,q):
while not q.empty():
temp_url=q.get(timeout=1)
try:
r=requests.get(temp_url,timeout=20)
print(threadname,r.status_code,temp_url)
except Exception as e:
print("Error",e)
pass
if __name__=='__main__':
start=time.time()
thread_list=[]
thread_Name=['Thread-1','Thread-2','Thread-3','Thread-4','Thread-5']
workQueue=Queue.Queue(1000)
#填充队列
for url in link_list:
workQueue.put(url)
#创建线程
for tname in thread_Name:
thread=MyThread(tname,workQueue)
thread.start()
thread_list.append(thread)
for t in thread_list:
t.join()
end=time.time()
print("All using time:",end-start)
print("Exiting Main Thread")
使用队列进行爬虫需要queue库,除去线程的知识,我们还需要队列的知识与之结合,上述代码中关键的队列知识有创建与填充队列,调用队列,持续使用队列3个,分别如下:
️:创建与队列:
workQueue=Queue.Queue(1000)
#填充队列
for url in link_list:
workQueue.put(url)
️:调用队列:
thread=MyThread(tname,workQueue)
️:持续使用队列:
def crawel(threadname,q):
while not q.empty():
pass
使用队列的思想就是先进先出,出完了就结束。
多进程爬虫:一般来说多进程爬虫有两种组合方式:multiprocessing和Pool+Queuex
muiltprocessing使用方法与thread并无多大差异,只需要替换部分代码即可,分别为进程的定义与初始化,以及进程的结束。
️:进程的定义与初始化:
class Myprocess(Process):
def __init__(self):
Process.__init__(self)
️:进程的递归结束:设置后当父进程结束后,子进程自动会被终止
p.daemon=True
另外一种方法是通过Manager和Pool结合使用
manager=Manager()
workQueue=manager.Queue(1000)
for url in link_list:
workQueue.put(url)
pool=Pool(processes=3)
for i in range(1,5):
pool.apply_async(crawler,args=(workQueue,i))
pool.close()
pool.join()
Python爬虫系统化学习(5)的更多相关文章
- Python爬虫系统化学习(4)
Python爬虫系统化学习(4) 在之前的学习过程中,我们学习了如何爬取页面,对页面进行解析并且提取我们需要的数据. 在通过解析得到我们想要的数据后,最重要的步骤就是保存数据. 一般的数据存储方式有两 ...
- Python爬虫系统化学习(2)
Python爬虫系统学习(2) 动态网页爬取 当网页使用Javascript时候,很多内容不会出现在HTML源代码中,所以爬取静态页面的技术可能无法使用.因此我们需要用动态网页抓取的两种技术:通过浏览 ...
- Python爬虫系统化学习(3)
一般来说当我们爬取网页的整个源代码后,是需要对网页进行解析的. 正常的解析方法有三种 ①:正则匹配解析 ②:BeatuifulSoup解析 ③:lxml解析 正则匹配解析: 在之前的学习中,我们学习过 ...
- Python爬虫系统学习(1)
Python爬虫系统化学习(1) 前言:爬虫的学习对生活中很多事情都很有帮助,比如买房的时候爬取房价,爬取影评之类的,学习爬虫也是在提升对Python的掌握,所以我准备用2-3周的晚上时间,提升自己对 ...
- 一个Python爬虫工程师学习养成记
大数据的时代,网络爬虫已经成为了获取数据的一个重要手段. 但要学习好爬虫并没有那么简单.首先知识点和方向实在是太多了,它关系到了计算机网络.编程基础.前端开发.后端开发.App 开发与逆向.网络安全. ...
- python爬虫专栏学习
知乎的一个讲python的专栏,其中爬虫的几篇文章,偏入门解释,快速看了一遍. 入门 爬虫基本原理:用最简单的代码抓取最基础的网页,展现爬虫的最基本思想,让读者知道爬虫其实是一件非常简单的事情. 爬虫 ...
- Python爬虫的学习经历
在准备学习人工智能之前呢,我看了一下大体的学习纲领.发现排在前面的是PYTHON的基础知识和爬虫相关的知识,再者就是相关的数学算法与金融分析.不过想来也是,如果想进行大量的数据运算与分析,宏大的基础数 ...
- python爬虫scrapy学习之篇二
继上篇<python之urllib2简单解析HTML页面>之后学习使用Python比较有名的爬虫scrapy.网上搜到两篇相应的文档,一篇是较早版本的中文文档Scrapy 0.24 文档, ...
- 【Python爬虫案例学习】下载某图片网站的所有图集
前言 其实很简短就是利用爬虫的第三方库Requests与BeautifulSoup. 其实就几行代码,但希望没有开发基础的人也能一下子看明白,所以大神请绕行. 基本环境配置 python 版本:2.7 ...
随机推荐
- cmd控制台Windows服务相关
1.创建服务 sc create ServerName binpath= "E:\MySql5.5\bin\mysqld.exe" 2.启动服务 sc start ServerNa ...
- 1.ASP.NET Core 管道、中间件、依赖注入
自定义中间件(基于工厂) 自定义中间件(注入到第三方容器)
- Linux ulimit使用
什么是ulimit? ulimit是一个可以设置或者汇报当前用户资源限制的命令.使用ulimit命令需要有管理员权限,它只能在允许使用shell进行控制的系统中使用.也就是说它已经被嵌入到shell当 ...
- Kafka官方文档V2.7
1.开始 1.1 简介 什么是事件流? 事件流相当于人体的中枢神经系统的数字化.它是 "永远在线 "世界的技术基础,在这个世界里,业务越来越多地被软件定义和自动化,软件的用户更是软 ...
- PAT L2-020 功夫传人【BFS】
一门武功能否传承久远并被发扬光大,是要看缘分的.一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱-- 直到某一支的某一代突然出现一个天分特别高的弟子(或者是吃到了灵丹.挖到 ...
- CodeForces - 13E(分块)
Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for on ...
- IDEA 安装常用操作一
关于IDEA的下载,破解自行百度 一.安装完成的常用设置 SDK选择.编译版本的选择,单项目选择,全局选择 maven配置,单项目,全局配置 二.IDEA如何安装lombok https://www. ...
- 使用 js 实现十大排序算法: 选择排序
使用 js 实现十大排序算法: 选择排序 选择排序 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
- 如何通过 terminal 查看一个文件的 meta信息
如何通过 terminal 查看一个文件的 meta 信息 Linux shell stat 查看文件 meta 信息 stat stat指令:文件/文件系统的详细信息显示: 使用格式:stat 文件 ...
- Flutter App 真机调试
Flutter App 真机调试 Deploy to iOS devices https://flutter.dev/docs/get-started/install/macos#deploy-to- ...