Python使用xpath实现图片爬取
作者:lurkerzhang 发布时间:2023-07-10 16:45:42
标签:Python,xpath,爬取
高性能异步爬虫
目的:在爬虫中使用异步实现高性能的数据爬取操作
异步爬虫的方式:
- 多线程、多进程(不建议):
好处:可以为相关阻塞的操作单独开启多线程或进程,阻塞操作就可以异步执行;
弊端:无法无限制的开启多线程或多进程。
- 线程池、进程池(适当的使用):
好处:我们可以降低系统对进程或线程创建和销毁的一个频率,从而很好的降低系统的开销;
弊端:池中线程或进程的数据是有上限的。
代码如下
# _*_ coding:utf-8 _*_
"""
@FileName :6.4k图片解析爬取(异步高性能测试).py
@CreateTime :2020/8/14 0014 10:01
@Author : Lurker Zhang
@E-mail : 289735192@qq.com
@Desc. :
"""
import requests
from lxml import etree
from setting.config import *
import json
import os
import time
from multiprocessing.dummy import Pool
def main():
# 图片采集源地址
# source_url = 'http://pic.netbian.com/4kmeinv/'
# temp_url = 'http://pic.netbian.com/4kmeinv/index_{}.html'
# source_url = 'http://pic.netbian.com/4kdongman/'
# temp_url = 'http://pic.netbian.com/4kdongman/index_{}.html'
source_url = 'http://pic.netbian.com/4kmingxing/'
temp_url = 'http://pic.netbian.com/4kmingxing/index_{}.html'
# 本此采集前多少页,大于1的整数
page_sum = 136
all_pic_list_url = []
if page_sum == 1:
pic_list_url = source_url
print('开始下载:' + pic_list_url)
all_pic_list_url.append(pic_list_url)
else:
# 先采集第一页
pic_list_url = source_url
# 调用采集单页图片链接的函数
all_pic_list_url.append(pic_list_url)
# 再采集第二页开始后面的页数
for page_num in range(2, page_sum + 1):
pic_list_url = temp_url.format(page_num)
all_pic_list_url.append(pic_list_url)
# 单页图片多线程解析
pool1 = Pool(10)
pool1.map(down_pic, all_pic_list_url)
print('采集完成,本地成功下载{0}张图片,失败{1}张图片。'.format(total_success, total_fail))
# 存储已下载文件名列表:
with open("../depository/mingxing/pic_name_list.json", 'w', encoding='utf-8') as fp:
json.dump(pic_name_list, fp)
def down_pic(pic_list_url):
print("准备解析图片列表页:",pic_list_url)
# 获取图片列表页的网页数据
pic_list_page_text = requests.get(url=pic_list_url, headers=headers).text
tree_1 = etree.HTML(pic_list_page_text)
# 获取图片地址列表
pic_show_url_list = tree_1.xpath('//div[@class="slist"]/ul//a/@href')
pic_url_list = [get_pic_url('http://pic.netbian.com' + pic_show_url) for pic_show_url in pic_show_url_list]
# 开始下载并保存图片(多线程)
pool2 = Pool(5)
pool2.map(save_pic, pic_url_list)
def save_pic(pic_url):
print("准备下载图片:",pic_url)
global total_success, total_fail, pic_name_list,path
picname = get_pic_name(pic_url)
if not picname in pic_name_list:
# 获取日期作为保存位置文件夹
pic = requests.get(url=pic_url, headers=headers).content
try:
with open(path + picname, 'wb') as fp:
fp.write(pic)
except IOError:
print(picname + "保存失败")
total_fail += 1
else:
pic_name_list.append(picname)
total_success += 1
print("成功保存图片:{0},共成功采集{1}张。".format(picname, total_success))
else:
print("跳过,已下载过图片:" + picname)
total_fail += 1
def get_pic_name(pic_url):
return pic_url.split('/')[-1]
def get_pic_url(pic_show_url):
tree = etree.HTML(requests.get(url=pic_show_url, headers=headers).text)
return 'http://pic.netbian.com/' + tree.xpath('//div[@class="photo-pic"]/a/img/@src')[0]
if __name__ == '__main__':
# 读入已采集图片的名称库,名称存在重复的表示已经采集过将跳过不采集
if not os.path.exists('../depository/mingxing/pic_name_list.json'):
with open("../depository/mingxing/pic_name_list.json", 'w', encoding="utf-8") as fp:
json.dump([], fp)
with open("../depository/mingxing/pic_name_list.json", "r", encoding="utf-8") as fp:
pic_name_list = json.load(fp)
path = '../depository/mingxing/' + time.strftime('%Y%m%d', time.localtime()) + '/'
if not os.path.exists(path):
os.mkdir(path)
# 记录本次采集图片的数量
total_success = 0
total_fail = 0
main()
来源:https://www.cnblogs.com/lurkerzhang/p/13561095.html


猜你喜欢
- 如果你有一字段dm记录了一个url,为了更好的优化模糊查询速度或统计速度,在数据表原有的结构上增加3个字段,分别为 `sdm`
- 一个简单的车牌输入组件(vue),供大家参考,具体内容如下代码:vue代码:<template> &l
- 今天在做python获取邮件时需要递归调用解析函数才可以解析邮件内容,最后想要将解析出的内容返回时发现返回的是None 可以内容却可以打印出
- 1. 引言本文重点介绍Python中的三个特殊函数Map,Filter和Reduce,以及如何使用它们进行代码编程。在开始介绍之前,我们先来
- 废话不多说了,直接把我写的timeit函数分享给大家,具体内容如下:/** * Compute the delay to execute a
- 本文实例为大家分析了javascript实现tab选项卡切换的调试笔记,供大家参考,具体内容如下制作导航栏,点击导航栏元素时下面的内容会产生
- pycharm中光标变粗,如下:原因:光标进入了改写状态。解决方法:按一下键盘中的Insert键就好了。
- 片头语:因为工作需要,在CentOS上搭建环境MySQL+Python+MySQLdb,个人比较习惯使用Windows系统的操作习惯,对纯字
- 本文实例为大家分享了python使用插值法画出平滑曲线的具体代码,供大家参考,具体内容如下实现所需的库numpy、scipy、matplot
- Sun Microsystems公司宣布,正式对外提供MySQL 5.1软件——这是全球最受欢迎的开
- 这里记录的主要是一张图,设计者是Adit Gupta。图中显示编程领域的先驱,以及各种编程语言的历史。很具有吸引力。
- Python 读取 .gz 文件读取.gz 文件需要使用gzip 包,如果没有安装可以自行在终端安装pip install gzipimpo
- 为什页面刷新会出现404因为vue项目中路由hash模式改为了history模式,由于hash模式时url带的#号后面是哈希值不会作为url
- 如何用下拉列表显示数据库里的内容? 我们来看看实现这个功能的程序:<%Dim objDC, objRSS
- 1.if ...else ...判断进行断言from time import *from selenium import webdriver
- random 模块中的常用函数random()返回一个位于区间 [0,1] 内的实数;uniform(a, b)返回一个位于区间 [a,b]
- DbUtils是Javar的一个为简化JDBC操作类库commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对
- tensorflow里面给出了一个函数用来读取图像,不过得到的结果是最原始的图像,是咩有经过解码的图像,这个函数为tf.gfile.Fast
- VScode 配置为 LaTeX IDE在Windows中,配置VScode作为LaTeX的编辑器(IDE),并使用SumatraPDF预览
- 在本篇文章当中主要给大家介绍一个我们在使用类的时候经常使用但是却很少在意的黑科技——描述器,在本篇文