首页 > 编程语言 >python爬虫单线程与多线程区别

python爬虫单线程与多线程区别

时间:2022-12-12 12:02:58浏览次数:59  
标签:font item python E6% datetime 单线程 href 多线程 class

之前有人请我帮忙写一个有关招聘的爬虫,一开始先是单线程,结果显而易见非常慢,后来改了多线程,速度杠杠的。

1、单线程

import urllib
import urllib.request
import requests
import xlwt
import re
import string

def set_style(name,height,bold=False):
style = xlwt.XFStyle() # 初始化样式

font = xlwt.Font() # 为样式创建字体
http://font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height

style.font = font

return style

index=0
r=0
wdk=xlwt.Workbook()
sheet1=wdk.add_sheet('sheet1',cell_overwrite_ok=True) #excel单元格名字
row=['招聘职位','月薪','学历要求','工作经验','应聘要求','工作单位']
for i in row:
sheet1.write(r,index,i)
index+=1
r+=1
import datetime

starttime = datetime.datetime.now()


for pn in range(0,200,20):
url = 'http://zhaopin.baidu.com/quanzhi?tid=4139&ie=utf8&oe=utf8&query=%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E5%B7%A5%E7%A8%8B%E5%B8%88&city_sug=%E9%95%BF%E6%B2%99&&detailmode=close&rn=20&pn='+str(pn)#打开网页
wp = urllib.request.urlopen(url)
content = wp.read()
hrefPatten=r"(href='.{0,200}数据挖掘工程师')"
href=hrefPatten.encode("utf-8")
hrefC = re.findall(href, content, re.S) #返回所有匹配正则表达式的值于列表中
hre=['']*len(hrefC)
for i in range (len(hrefC)):
hre[i]=urllib.parse.unquote(str(hrefC[i].decode('utf-8')).replace("href='","http://zhaopin.baidu.com")[:-1])#将href转化为网址

for i in range (len(hre)):
a=requests.get(hre[i])

content1=a.content.decode('utf-8')
pattern=('<span class="title line-clamp1">(.*?)</span>.*?class="salary">(.*?)</span>.*?class="xueli"></span>(.*?)</li>.*?class="minge"></span>(.*?)</li>.*?class="duty duty-box">(.*?)</p>.*?class="line-clamp2">(.*?)</p>')

items=re.findall(pattern, content1, re.S)#抓取招聘信息
for item in items:
flag=0
for _item in item:
if _item=="":
flag=1
if flag==0:
for k in range(0,6):
sheet1.write(r,k,item[k])
r+=1

wdk.save('/home/y.xls')
endtime = datetime.datetime.now()

print((endtime - starttime).seconds)

用时80S完成。

2、多线程

import urllib
import urllib.request
import requests
import xlwt
import re
import string
import threading
from time import ctime,sleep

def set_style(name,height,bold=False):
style = xlwt.XFStyle() # 初始化样式

font = xlwt.Font() # 为样式创建字体
http://font.name = name # 'Times New Roman'
font.bold = bold
font.color_index = 4
font.height = height

style.font = font

return style

import datetime
starttime = datetime.datetime.now()

index=0
global r
r=0
wdk=xlwt.Workbook()
sheet1=wdk.add_sheet('sheet1',cell_overwrite_ok=True) #excel单元格名字
row=['招聘职位','月薪','学历要求','工作经验','应聘要求','工作单位']
for i in row:
sheet1.write(r,index,i)
index+=1
r+=1
inte=[]
def f(pn):
url = 'http://zhaopin.baidu.com/quanzhi?tid=4139&ie=utf8&oe=utf8&query=%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98%E5%B7%A5%E7%A8%8B%E5%B8%88&city_sug=%E9%95%BF%E6%B2%99&&detailmode=close&rn=20&pn='+str(pn)#打开网页
wp = urllib.request.urlopen(url)
content = wp.read()
hrefPatten=r"(href='.{0,200}数据挖掘工程师')"
href=hrefPatten.encode("utf-8")
hrefC = re.findall(href, content, re.S) #返回所有匹配正则表达式的值于列表中
hre=['']*len(hrefC)
for i in range (len(hrefC)):
hre[i]=urllib.parse.unquote(str(hrefC[i].decode('utf-8')).replace("href='","http://zhaopin.baidu.com")[:-1])#将href转化为网址
for i in range (len(hre)):
a=requests.get(hre[i])

content1=a.content.decode('utf-8')
pattern=('<span class="title line-clamp1">(.*?)</span>.*?class="salary">(.*?)</span>.*?class="xueli"></span>(.*?)</li>.*?class="minge"></span>(.*?)</li>.*?class="duty duty-box">(.*?)</p>.*?class="line-clamp2">(.*?)</p>')

items=re.findall(pattern, content1, re.S)#抓取招聘信息
for item in items:
flag=0
for _item in item:
if _item=="":
flag=1
if flag==0:
inte.append(item)
endtime = datetime.datetime.now()
print((endtime - starttime).seconds)

threads = []
for pn in range(0,200,20):
threads.append(threading.Thread(target=f,args=(pn,)))
sleep(0.1)

import datetime

starttime = datetime.datetime.now()

if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()

sleep(15)
ind=1
for item in inte:
for k in range(0,6):
sheet1.write(ind,k,item[k])
ind+=1
wdk.save('/home/t.xls')

用时只要15S。

标签:font,item,python,E6%,datetime,单线程,href,多线程,class
From: https://blog.51cto.com/u_13488918/5929336

相关文章

  • Python爬虫实战,requests+openpyxl模块,爬取手机商品信息数据(附源码)
    前言今天给大家介绍的是Python爬取手机商品信息数据,在这里给需要的小伙伴们代码,并且给出一点小心得。首先是爬取之前应该尽可能伪装成浏览器而不被识别出来是爬虫,基本的......
  • 力扣852(java&python3)-山脉数组的峰顶索引(中等)
    题目:符合下列属性的数组arr称为山脉数组:arr.length>=3存在i(0<i <arr.length-1)使得:arr[0]<arr[1]<...arr[i-1]<arr[i]arr[i]>arr[i+1]>...>......
  • python如何开发一个GUI图形应用?
    需求:我想学习,python如何开发一个GUI图形应用?解决:总共分两部分:前端ui和后端逻辑py。 前端ui1.导入模块pipinstallPySide22.找到designer.exe 打开......
  • 用Python将图片转为字符画
    今天打算玩个好玩的,也是基于一个优秀的图像处理库——PIL,使用ascii字符把图片转为黑白字符画。首先有个问题,就是模拟灰度,这里有个公式:Gray=0.2126×R+0.7152×G+0......
  • 详解Python 3.8的海象算子:大幅提高程序执行效率
    ​作者:AnimeshGaitonde机器之心编译参与:Panda前几个月发布的Python3.8包含了一项重要的新功能,即海象算子。如果合理运用,该算子能有效地提升Python程序的执行效率。本......
  • C++爬虫如何进行多线程调试
    我们知道在爬虫钱进行多线程调试是非常重要的,之前我们也有讨论过程序调试,今天我们还将继续在这里深入的讲解下软件调试的一些内容。比如说常见的条件断点,数据断点,多线程断点......
  • 多线程爬虫如何实现线程安全?
    做大数据抓取的应当都知道,多线程爬取数据能够有效的提供工作效率、降低运营成本。那么在编程爬虫代码的时候如果保证线程安全呢?下面我们就来聊一聊。1、什么是线程安全问题......
  • python for-else break continue应用
    ##for临时变量in序列:##重复执行的代码##......##else:##循环正常结束后要执行的代码#所谓else指的是循环正常结束后要执行的代码,即如果是bresk终止......
  • Python 基于xml.etree.ElementTree实现XML对比
    测试环境Python3.6Win10代码实现#!/usr/bin/envpython3.4.0#-*-encoding:utf-8-*-__author__='shouke'importxml.etree.ElementTreeasETdefcompare_......
  • python高性能异步爬虫
    目的:在爬虫中使用异步实现高性能的数据爬取操作。异步爬虫的方式:1、多线程,多进程(不建议):好处:可以为相关阻塞的操作单独开启线程,阻塞操作就可以异步执行。弊端:无法无限制的开......