import os
import uuid
from lxml import html
import aiofiles
import logging
from ruia import Spider, Request
from ruia_ua import middleware
from aiohttp探究.db import MotorBase
import datetime demo = "https://www.mzitu.com/page/{}/" class BaiduImgSpider(Spider):
start_urls = []
img_path = 'data/' async def parse(self, res):
self.mongo_db = MotorBase().get_db('img_data')
source = res.html
root = html.fromstring(source)
url_list = root.xpath("//ul[@id='pins']/li/a/@href")
name_list = root.xpath("//ul[@id='pins']/li/a/img/@alt")
next_page_urls = []
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'referer': 'https://www.mzitu.com/mm/',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
}
for each_data in url_list:
next_page_urls.append(each_data)
for name, url in zip(name_list, next_page_urls):
yield Request(url, headers=headers, callback=self.next_page, metadata={"name": name}, res_type='text') async def next_page(self, res):
source = res.html
root = html.fromstring(source)
name = res.metadata.get("name")
refere_url = res.url
# print(name, refere_url)
# 最后一页xpath
max_page_list = "//div[@class='pagenavi']/a[last()-1]/span/text()"
_max_page_num = root.xpath(max_page_list)
max_page_num = _max_page_num[0] if _max_page_num else None
img_url_node = root.xpath("//div[@class='main-image']/p/a/img/@src")
img_url = img_url_node[0] if img_url_node else None
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'max-age=0',
'if-modified-since': 'Thu, 15 Nov 2018 04:24:11 GMT',
'if-none-match': '"5becf4eb-1b7d4"',
'referer': refere_url,
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
}
datas = []
# yield Request(img_url, callback=self.save_img, headers=headers,
# metadata={"url": img_url, "name": name, "id": "1"},
# res_type='bytes')
data1 = {'url': img_url, "status": "0", 'title': name, "img_id": "1", "headers": headers,
"crawler_date": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} datas.append(data1)
# print("最大页数", max_page_num) for page in range(2, int(max_page_num) + 1):
headers["referer"] = f"{refere_url}{str(page).zfill(2)}"
next_img_url = img_url.replace("01.", f"{str(page).zfill(2)}.")
# print("next",next_img_url)
# yield Request(next_img_url, callback=self.save_img, headers=headers,
# metadata={"url": img_url, "name": name, "id": page},
# res_type='bytes')
data2 = {'url': next_img_url, "status": "0", 'title': name, "img_id": page, "headers": headers,
"crawler_date": datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
datas.append(data2)
await self.mongo_db.mzitu2.insert_many(datas) async def save_img(self, res):
url = res.metadata.get("url")
_img_type = url.rsplit(".", 1)
img_type = _img_type[1] if _img_type else None
name = res.metadata.get("name")
img_id = res.metadata.get("id")
img_all_path = f"{self.img_path}{name}/"
if not os.path.exists(img_all_path):
os.makedirs(img_all_path)
# img_name = str(uuid.uuid1()) + "_" + res.url[-10:].replace('/', '-')
img_name = f"{img_id}.{img_type}"
async with aiofiles.open(img_all_path + img_name, 'wb') as fp:
await fp.write(res.html)
logging.info('Img downloaded successfully in {dir}'.format(dir=img_all_path + img_name)) if __name__ == '__main__':
word = '妹子图' # 目录名
pages = 201 # 页数
BaiduImgSpider.img_path = word + "/"
BaiduImgSpider.start_urls = [demo.format(page) for page in range(pages)]
BaiduImgSpider.start(middleware=middleware)

db.py

import asyncio

from motor.motor_asyncio import AsyncIOMotorClient

class MotorBase:
"""
About motor's doc: https://github.com/mongodb/motor
"""
_db = {}
_collection = {} def __init__(self, loop=None):
self.motor_uri = ''
self.loop = loop or asyncio.get_event_loop() def client(self, db):
# motor
self.motor_uri = f"mongodb://localhost:27017/{db}"
return AsyncIOMotorClient(self.motor_uri, io_loop=self.loop) def get_db(self, db='test'):
"""
Get a db instance
:param db: database name
:return: the motor db instance
"""
if db not in self._db:
self._db[db] = self.client(db)[db] return self._db[db]

福利爬虫妹子图之获取种子url的更多相关文章

  1. 关于如何爬虫妹子图网的源码分析 c#实现

    网上也出现一些抓取妹子图的python 代码,今天我们用c#实现爬虫过程. 请看我的网站: www.di81.com private void www_94xmn_Com(string url, st ...

  2. 爬虫实战【5】送福利!Python获取妹子图上的内容

    [插入图片,妹子图首页] 哈,只敢放到这个地步了. 今天给直男们送点福利,通过今天的代码,可以把你的硬盘装的满满的~ 下面就开始咯! 第一步:如何获取一张图片 假如我们知道某张图片的url,如何获取到 ...

  3. python妹子图爬虫5千张高清大图突破防盗链福利5千张福利高清大图

    meizitu-spider python通用爬虫-绕过防盗链爬取妹子图 这是一只小巧方便,强大的爬虫,由python编写 所需的库有 requests BeautifulSoup os lxml 伪 ...

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

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

  5. Scrapy框架实战-妹子图爬虫

    Scrapy这个成熟的爬虫框架,用起来之后发现并没有想象中的那么难.即便是在一些小型的项目上,用scrapy甚至比用requests.urllib.urllib2更方便,简单,效率也更高.废话不多说, ...

  6. Python爬虫入门教程 2-100 妹子图网站爬取

    妹子图网站爬取---前言 从今天开始就要撸起袖子,直接写Python爬虫了,学习语言最好的办法就是有目的的进行,所以,接下来我将用10+篇的博客,写爬图片这一件事情.希望可以做好. 为了写好爬虫,我们 ...

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

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

  8. [Python爬虫]煎蛋网OOXX妹子图爬虫(1)——解密图片地址

    之前在鱼C论坛的时候,看到很多人都在用Python写爬虫爬煎蛋网的妹子图,当时我也写过,爬了很多的妹子图片.后来煎蛋网把妹子图的网页改进了,对图片的地址进行了加密,所以论坛里面的人经常有人问怎么请求的 ...

  9. Python Scrapy 爬取煎蛋网妹子图实例(一)

    前面介绍了爬虫框架的一个实例,那个比较简单,这里在介绍一个实例 爬取 煎蛋网 妹子图,遗憾的是 上周煎蛋网还有妹子图了,但是这周妹子图变成了 随手拍, 不过没关系,我们爬图的目的是为了加强实战应用,管 ...

随机推荐

  1. 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试

    前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...

  2. loj #116. 有源汇有上下界最大流

    题目链接 有源汇有上下界最大流,->上下界网络流 注意细节,重置cur和dis数组时,有n+2个点 #include<cstdio> #include<algorithm> ...

  3. 洛谷P4413 R2

    好,这是一道巨水题...... #include <cstdio> using namespace std; typedef long long LL; int main() { LL a ...

  4. JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案

    本项目测试环境 JDK: 1.8 SpringBoot: 2.1 需求描述 当我们想要利用SpringBoot封装一套组件并发布给第三方使用时,我们就不得不考虑我们的组件能否被使用者正确引入使用,此处 ...

  5. __slots__,__doc__,__del__,__call__,__iter__,__next__迭代器协议(三十六)

    1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的__dic ...

  6. You are using safe update mode and you tried to update a table--mysql

    SET SQL_SAFE_UPDATES = 0;delete from cms_article_data where id in(SELECT id FROM jeesite.cms_article ...

  7. (大数 string) Integer Inquiry hdu1047

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Grafana的基本使用

    Grafana的基本使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 简单的来说,Grafana 是基于JS开发的,功能齐全的度量仪表盘和图形编辑器,帮助开发人员发现问题的工具. ...

  9. CentOS 6.9/7通过yum安装指定版本的MySQL

    一.安装CENTOS 6 # wget http://repo.mysql.com/mysql57-community-release-el6.rpm && rpm -ivh mysq ...

  10. CM记录-集群主机存储情况