Python多线程爬虫爬取网页图片
临近期末考试,但是根本不想复习!啊啊啊啊啊啊啊!!!!
于是做了一个爬虫,网址为 https://yande.re,网页图片为动漫美图(图片带点颜色........宅男福利
github项目地址为:https://github.com/MyBules/yande_pider
多线程代码分为两个版本:一个是基于多页面多线程,一个是基于单页面多线程
一下是第一种代码:
'''
基于多页面多线程
'''
import os # 引入文件模块
import re # 正则表达式
import urllib.request
import threading # 连接网页并返回源码
def open_url(url):
try:
req = urllib.request.Request(url)
req.add_header("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
response = urllib.request.urlopen(req)
status_code = response.code
html = response.read()
return html
except:
print(url + "")
return 404 def mkdir(path):
'''
:param path: 路径
:return:
'''
# 引入模块
import os # 去除首位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在
# 存在 True
# 不存在 False
isExists = os.path.exists(path) # 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
print(path + ' 创建成功')
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print(path + ' 目录已存在')
return False def Yande1(i):
imgs = 1
url = 'https://yande.re/post?page=' + str(i)
floder = "E:\\Python\\爬虫\\yande\\img\\page" + str(i)
mkdir(floder) html = open_url(url)
html = html.decode('gbk', 'ignore')
img_adds = []
img_adds = re.findall(r'<a class="directlink largeimg" href="([^"]+\.jpg)"', html)
for i in img_adds:
filename = floder + "\\" + str(imgs) + '.jpg'
imgs += 1
img_html = open_url(i)
if img_html == 404:
continue
with open(filename, 'wb') as f:
f.write(img_html)
print(i + ' 下载完成......') exitflag = 0 class myThread(threading.Thread):
def __init__(self, threadID, name, list):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.list = list
def run(self):
print("开始线程:" + self.name)
# threadLock.acquire()
get_img(self.name, self.list)
# threadLock.release()
print("退出线程:"+ self.name) def get_img(threadname, list):
if len(list):
for i in list:
if exitflag:
threadname.exit()
Yande1(i) if __name__ == '__main__':
pages1 = int(input('请输入你要下载的起始页面数:'))
pages2 = int(input('请输入你要下载的末尾页面数:'))
mkdir('img') # for i in range()
list1 = []
list2 = []
list3 = []
for i in range(pages1, pages2+1):
if i % 3 == 0:
list3.append(i)
if i % 3 == 1:
list1.append(i)
if i % 3 == 2:
list2.append(i)
threadLock = threading.Lock()
threads = []
thread1 = myThread(1, "thread-1", list1)
thread2 = myThread(2, "thread-2", list2)
thread3 = myThread(3, "thread-3", list3)
thread1.start()
thread2.start()
thread3.start()
threads.append(thread1)
threads.append(thread2)
threads.append(thread3) for t in threads:
t.join()
print("退出主线程")
经测试,两种方法速度相差不大。
第二种方法放在github项目地址里了,如果各位游客是为了学习的话,第二种方法的代码还是去看一下较好。
Python多线程爬虫爬取网页图片的更多相关文章
- node:爬虫爬取网页图片
代码地址如下:http://www.demodashi.com/demo/13845.html 前言 周末自己在家闲着没事,刷着微信,玩着手机,发现自己的微信头像该换了,就去网上找了一下头像,看着图片 ...
- erlang 爬虫——爬取网页图片
说起爬虫,大家第一印象就是想到了python来做爬虫.其实,服务端语言好些都可以来实现这个东东. 在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌 ...
- Python爬虫爬取网页图片
没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来. 今天逛贴吧看见好多美图,可是图片有点多,不想一张一张地复制粘贴 ...
- Python多线程爬虫爬取电影天堂资源
最近花些时间学习了一下Python,并写了一个多线程的爬虫程序来获取电影天堂上资源的迅雷下载地址,代码已经上传到GitHub上了,需要的同学可以自行下载.刚开始学习python希望可以获得宝贵的意见. ...
- java爬虫-简单爬取网页图片
刚刚接触到“爬虫”这个词的时候是在大一,那时候什么都不明白,但知道了百度.谷歌他们的搜索引擎就是个爬虫. 现在大二.再次燃起对爬虫的热爱,查阅资料,知道常用java.python语言编程,这次我选择了 ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
- python requests库爬取网页小实例:爬取网页图片
爬取网页图片: #网络图片爬取 import requests import os root="C://Users//Lenovo//Desktop//" #以原文件名作为保存的文 ...
- [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】
[python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...
- python3爬虫爬取网页思路及常见问题(原创)
学习爬虫有一段时间了,对遇到的一些问题进行一下总结. 爬虫流程可大致分为:请求网页(request),获取响应(response),解析(parse),保存(save). 下面分别说下这几个过程中可以 ...
随机推荐
- OC入门笔记
1OC概述OC主要负责UI界面:C语言和C++可以用于图形处理.OC是一门面向对象的语言.C语言是面向过程的.比C++简单很多以C语言为基础,完全兼容C语言.OC语言中的所有事物都是对象,都有isa指 ...
- vue项目中导出Excel文件功能的前端代码实现
在项目中遇到了两种不同情况, 1.get请求导出文件,实现起来相对简单 // 导出数据 exportData() { window.location.href = `/oes-content-mana ...
- Centos安装Python3及设置对应版本pip
安装Python3 安装Python依赖: yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel ...
- sklearn.feature_extraction.text.CountVectorizer 学习
CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...
- python 脚本bak文件还原mssql数据库
# -*- coding=utf-8 -*- import pyodbc from datetime import datetime import pymssql import decimal cla ...
- oracle管道函数的用法(一行拆为多行)
oracle管道函数是一类特殊的函数,oracle管道函数返回值类型必须为集合 如果需要在客户端实时的输出函数执行过程中的一些信息,在oracle9i以后可以使用管道函数(pipeline funct ...
- Timsort
https://archive.codeplex.com/?p=timsort4net#117964 download archive https://codeplexarchive.blob.cor ...
- 基于java配置log4j日志详解
1.Log4j 1.1了解Log4j Log4j是Apache的一个开源项目,通过使用log4j,我们可以控制日志信息输送的目的地可以是控制台.文件.GUI组件,我们也可以控制每一条日志的输出格式,通 ...
- 【坑】Mybatis 多次逆向工程生成mapper文件
在使用 mybatis 逆向工程的时候,多次逆向工程生成的文件,是不会产生覆盖的,而是追加: 假如,你第一次逆向,发生数据库的某个字段类型错了,修改以后再次逆向,那么得到的 mapper文件,将是 2 ...
- Python 基础教程 | 菜鸟教程
https://www.runoob.com/python/python-install.html