一、参考文章

Python爬虫之——爬取妹子图片

上述文章中的代码讲述的非常清楚,我的基本能思路也是这样,本篇文章中的代码仅仅做了一些异常处理和一些日志显示优化工作,写此文章主要是当做笔记,方便以后查阅,修改的地方如下:

1、异常处理下面在代码中会单独标红

2、多线程版使用了multiprocessing这个库,需要在main函数开始调用freeze_support(),防止打包成exe之后,运行时创建线程失败

3、多线程版本加了一个命令行自定义线程个数功能

二、单线程版本

 #coding=utf-8
import requests
from bs4 import BeautifulSoup
import os all_url = 'http://www.mzitu.com' #http请求头
Hostreferer = {
'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Referer':'http://www.mzitu.com'
}
Picreferer = {
'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Referer':'http://i.meizitu.net'
}
#此请求头破解盗链 start_html = requests.get(all_url, headers = Hostreferer) #保存地址
path = os.getcwd() + '/mzitu/' #找寻最大页数
soup = BeautifulSoup(start_html.text, "html.parser")
page = soup.find_all('a', class_='page-numbers')
max_page = page[-2].text same_url = 'http://www.mzitu.com/page/'
for n in range(0, int(max_page)+1):#遍历页面数
ul = same_url+str(n)
start_html = requests.get(ul, headers = Hostreferer)
soup = BeautifulSoup(start_html.text, "html.parser")
all_a = soup.find('div', class_ = 'postlist').find_all('a', target = '_blank')
for a in all_a:#每个页面包含的妹子数
title = a.get_text() #提取文本
if(title != ''):
print("准备扒取:" + title) #win不能创建带?的目录
if(os.path.exists(path+title.strip().replace('?', ''))):
#print('目录已存在')
flag = 1
else:
os.makedirs(path+title.strip().replace('?', ''))
flag = 0
os.chdir(path + title.strip().replace('?', ''))
href = a['href']
html = requests.get(href, headers = Hostreferer)
mess = BeautifulSoup(html.text, "html.parser")
pic_max = mess.find_all('span')
pic_max = pic_max[10].text #最大页数
if(flag == 1 and len(os.listdir(path+title.strip().replace('?', ''))) >= int(pic_max)):
print('已经保存完毕,跳过')
continue
for num in range(1, int(pic_max) + 1):#每个妹子的所有照片
pic = href+'/'+str(num)
html = requests.get(pic, headers = Hostreferer)
mess = BeautifulSoup(html.text, "html.parser")
pic_url = mess.find('img', alt = title) if 'src' not in pic_url.attrs:#有些pic_url标签没有src这个属性,导致操作异常,在次进行过滤
continue
print(pic_url['src'])
#exit(0)
html = requests.get(pic_url['src'],headers = Picreferer)
file_name = pic_url['src'].split(r'/')[-1]
f = open(file_name, 'wb')
f.write(html.content)
f.close()
print('完成')
print('第',n,'页完成')

三、多线程版本

 #coding=utf-8
import requests
from bs4 import BeautifulSoup
import os
from multiprocessing import Pool
from multiprocessing import freeze_support
import sys header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36',
'Referer':'http://www.mzitu.com'
}
Picreferer = {
'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Referer':'http://i.meizitu.net'
} def find_MaxPage():
all_url = 'http://www.mzitu.com'
start_html = requests.get(all_url, headers = header)
#找寻最大妹子页面数
soup = BeautifulSoup(start_html.text, "html.parser")
page = soup.find_all('a', class_ = 'page-numbers')
max_page = page[-2].text
return max_page def Download(href, title, path):
html = requests.get(href, headers = header)
soup = BeautifulSoup(html.text, 'html.parser')
pic_max = soup.find_all('span')
pic_max = pic_max[10].text # 最大页数
if(os.path.exists(path+title.strip().replace('?', ''))
and len(os.listdir(path+title.strip().replace('?', ''))) >= int(pic_max)):
print('妹子已待命,继续准备下一个妹子' + title)
return 1
print(f"发现妹子资源{pic_max}个,准备中:" + title)
os.makedirs(path + title.strip().replace('?', ''))
os.chdir(path + title.strip().replace('?', ''))
for num in range(1, int(pic_max) + 1):
pic = href + '/' + str(num)
html = requests.get(pic, headers = header)
mess = BeautifulSoup(html.text, "html.parser")
pic_url = mess.find('img', alt = title)
if 'src' not in pic_url.attrs:#有些pic_url标签没有src属性,导致操作异常,在次进行过滤
continue
print(f"{title}:{pic_url['src']}")
html = requests.get(pic_url['src'], headers = header)
file_name = pic_url['src'].split(r'/')[-1]
f = open(file_name,'wb')
f.write(html.content)
f.close()
print('妹子已就绪,客官请慢用:' + title) if __name__ == '__main__':
freeze_support()#防止打包后 运行exe创建进程失败 #线程池中线程数
count = 1
if len(sys.argv) >=2:
count = int(sys.argv[1]) pool = Pool(count)
print(f'初始化下载线程个数${count}') # http请求头
path = os.getcwd() + '/mzitu_mutil/'
max_page = find_MaxPage() #获取最大页数 即生成的文件夹数量
print(f'捕获{max_page}页妹子,请耐心等待下载完成')
same_url = 'http://www.mzitu.com/page/' for n in range(1, int(max_page) + 1):
each_url = same_url + str(n)
start_html = requests.get(each_url, headers = header)#请求一页中的所有妹子
soup = BeautifulSoup(start_html.text, "html.parser")
all_a = soup.find('div', class_ = 'postlist').find_all('a', target = '_blank')
for a in all_a:#遍历每一页中的妹子
title = a.get_text() # 提取文本
if (title != ''):
href = a['href']#请求妹子的所有图集
pool.apply_async(Download, args = (href, title, path))
pool.close()
pool.join()
print('所有妹子已就绪,客官请慢用')

四、资源下载

  资源下载地址:Python爬取妹子图-单线程和多线程版本

转载声明:本站文章无特别说明,皆为原创,版权所有,转载请注明:朝十晚八

Python-爬取妹子图(单线程和多线程版本)的更多相关文章

  1. python爬取妹子图全站全部图片-可自行添加-线程-进程爬取,图片去重

    from bs4 import BeautifulSoupimport sys,os,requests,pymongo,timefrom lxml import etreedef get_fenlei ...

  2. Python 爬取 妹子图(技术是无罪的)

    ... #!/usr/bin/env python import urllib.request from bs4 import BeautifulSoup def crawl(url): header ...

  3. Python 爬取妹子图(技术是无罪的)

    ... import requests from bs4 import BeautifulSoup import os import sys class mzitu(): def html(self, ...

  4. Python协程爬取妹子图(内有福利,你懂得~)

    项目说明: 1.项目介绍   本项目使用Python提供的协程+scrapy中的选择器的使用(相当好用)实现爬取妹子图的(福利图)图片,这个学会了,某榴什么的.pow(2, 10)是吧! 2.用到的知 ...

  5. Python3爬虫系列:理论+实验+爬取妹子图实战

    Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...

  6. python 爬取妹子

    爬取妹子图片 网址:https://www.mzitu.com/jiepai/ 2019-06-13 环境WIN10 1903 python 3.7.3 个人习惯先在IDLE中进行调试 import ...

  7. Python网络爬虫 | Scrapy爬取妹子图网站全站照片

    根据现有的知识,写了一个下载妹子图(meizitu.com)Scrapy脚本,把全站两万多张照片下载到了本地. 网站的分析 网页的网址分析 打开网站,发现网页的网址都是以 http://www.mei ...

  8. python爬取斗图网中的 “最新套图”和“最新表情”

    1.分析斗图网 斗图网地址:http://www.doutula.com 网站的顶部有这两个部分: 先分析“最新套图” 发现地址栏变成了这个链接,我们在点击第二页 可见,每一页的地址栏只有后面的pag ...

  9. Python爬取 斗图表情,让你成为斗图大佬

    话不多说,上结果(只爬了10页内容) 上代码:(可直接运行)   用到Xpath #encoding:utf-8 # __author__ = 'donghao' # __time__ = 2018/ ...

随机推荐

  1. 批处理(Batch)---批处理脚本。

    批处理(Batch),也称为批处理脚本.顾名思义,批处理就是对某对象进行批量的处理,通常被认为是一种简化的脚本语言,它应用于DOS和Windows系统中.批处理文件的扩展名为bat .目前比较常见的批 ...

  2. java项目获取根路径(web项目和application项目的区分)

    Java项目中经常要读取配置文件,涉及到读取配置文件的地方,就会要读定位文件的路径.因此,在项目如何正确获取文件路径尤为关键. 根据不同的java项目,在获取文件路径时候有一些 小区别 测试环境:E: ...

  3. Flask信号和wtforms

    一.信号 1.1.所有内置信号 request_started = _signals.signal('request-started') # 请求到来前执行 request_finished = _s ...

  4. python_code list_3

    >>> seq=['foo','x41','?','***']>>> def func(x): return x.isalnum() >>> li ...

  5. Selenium自动化测试-unittest单元测试框架

    一.Pyhon工作原理-- 核心概念:test case, testsuite, TestLoder,TextTestRunner,TextTestResult, test fixture TestC ...

  6. linux 获取网络状态信息(Rtnetlink)

    一.Rtnetlink Rtnetlink 允许对内核路由表进行读和更改,它用于内核与各个子系统之间(路由子系统.IP地址.链接参数等)的通信, 用户空间可以通过NET_LINK_ROUTER soc ...

  7. RestTemplate 微信接口 text/plain HttpMessageConverter

    一.背景介绍 使用 Spring Boot 写项目,需要用到微信接口获取用户信息. 在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然 ...

  8. 自动化测试--protractor

    前戏 面向模型编程: 测试驱动开发: 先保障交互逻辑,再调整细节.---by 雪狼. 为什么要自动化测试? 1,提高产出质量. 2,减少重构时的痛.反正我最近重构多了,痛苦经历多了. 3,便于新人接手 ...

  9. Vue路由学习心得

    GoodBoy and GoodGirl~进来了就看完点个赞再离开,写了这么多也不容易的~ 一.介绍  1.概念:路由其实就是指向的意思,当我们点击home按钮时,页面中就要显示home的内容,点击l ...

  10. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...