Scrapy 爬取某网站图片
1. 创建一个 Scrapy 项目,在命令行或者 Pycharm 的 Terminal 中输入:
scrapy startproject imagepix
自动生成了下列文件:
2. 在 imagepixiv/spiders 文件夹下新建一个 imagepixiv.py 文件
3. imagepixiv.py 下的代码:
import scrapy from urllib.parse import urlencode import json from ..items import ImagepixItem class ImagepixivSpider(scrapy.Spider): name = 'imagepixiv' def start_requests(self): data = {'keyword': '风景'} base_url_1 = 'https://api.pixivic.com/illustrations?' for page in range(1, self.settings.get('MAX_PAGE') + 1): data['page'] = page params = urlencode(data) url_1 = base_url_1 + params yield scrapy.Request(url_1, callback=self.parse) def parse(self, response): result = json.loads(response.text) for image in result.get('data'): item = ImagepixItem() item['title'] = image.get('title') item['id'] = image.get('id') url = image.get('imageUrls')[0].get('large') url_rel = 'https://img.pixivic.com:23334/get/' + str(url) item['url'] = url_rel yield item
4. items.py 下的代码:
import scrapy from scrapy import Field class ImagepixItem(scrapy.Item): title = Field() id = Field() url = Field()
5. pipelines.py 下的代码:
from scrapy import Requestfrom scrapy.exceptions import DropItemfrom scrapy.pipelines.images import ImagesPipeline class ImagepixPipeline(ImagesPipeline): def file_path(self, request, response=None, info=None): url = request.url file_name = url.split('/')[-1] return file_name def item_completed(self, results, item, info): image_paths = [x['path'] for ok, x in results if ok] if not image_paths: raise DropItem('Image Downloaded Failed') return item def get_media_requests(self, item, info): yield Request(item['url'])
6. settings.py 下的代码:
BOT_NAME = 'imagepix' SPIDER_MODULES = ['imagepix.spiders'] NEWSPIDER_MODULE = 'imagepix.spiders' MAX_PAGE = 50 FEED_EXPORT_ENCODING = 'utf-8' IMAGES_STORE = './images' ITEM_PIPELINES = { 'imagepix.pipelines.ImagepixPipeline': 300, } ROBOTSTXT_OBEY = False
7. 在命令行运行:
scrapy crawl imagepixiv
8. 结果:
Scrapy 爬取某网站图片的更多相关文章
- 用scrapy爬取搜狗Lofter图片
用scrapy爬取搜狗Lofter图片 # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Reques ...
- Java爬虫实践--爬取CSDN网站图片为例
实现的效果,自动在工程下创建Pictures文件夹,根据网站URL爬取图片,层层获取.在Pictures下以网站的层级URL命名文件夹,用来装该层URL下的图片.同时将文件名,路径,URL插入数据库, ...
- scrapy爬取某网站,模拟登陆过程中遇到的那些坑
本节内容 在访问网站的时候,我们经常遇到有些页面必须用户登录才能访问.这个时候我们之前写的傻傻的爬虫就被ban在门外了.所以本节,我们给爬虫配置cookie,使得爬虫能保持用户已登录的状态,达到获得那 ...
- python3 urllib爬取wallhalla网站图片
点我去我的github上看源码 简单使用静态方法爬取https://wallhalla.com/网站的图片 参考: https://blog.csdn.net/cquptcmj/article/det ...
- 使用nodejs+http(s)+events+cheerio+iconv-lite爬取2717网站图片数据到本地文件夹
源代码如下: //(node:9240) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' ...
- 使用Scrapy爬取图书网站信息
重难点:使用scrapy获取的数值是unicode类型,保存到json文件时需要特别注意处理一下,具体请参考链接:https://www.cnblogs.com/sanduzxcvbnm/p/1030 ...
- python3爬取动态网站图片
思路: 1.图片放在<image>XXX</image>标签中 2.利用fiddler抓包获取存放图片信息的js文件url 3.利用requests库获取html内容,然后获取 ...
- 实战爬取某网站图片-Python
直接上代码 1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 from bs4 import BeautifulSoup 4 import request ...
- scrapy爬取招聘网站,items转换成dict遇到的问题
pipelines代码 1 import json 2 3 class TencentJsonPipeline(object): 4 def __init__(self): 5 self.file = ...
随机推荐
- SpringCloud(2)----SpringCloud理解篇
一.微服务概述 1.什么是微服务 目前的微服务并没有一个统一的标准,一般是以业务来划分将传统的一站式应用,拆分成一个个的服务,彻底去耦合,一个微服务就是单功能业务,只做一件事. 与微服务相对的叫巨石 ...
- 【11】ajax请求后台接口数据与返回值处理js写法
$.ajax({ url: "/test.php",//后台提供的接口 type: "post", //请求方式是post data:{"type ...
- SMARTY核心
http://www.smarty.net/http://smarty.php.net/manual/en/ 1.配置 define("ROOTPATH",dirname(__FI ...
- Visual Studio工具
下载地址: https://code.visualstudio.com 插件安装: http://blog.csdn.net/u011127019/article/details/53158660 下 ...
- linux之反向代理,反向代理实例,负载均衡实例
目录 nginx反向代理 1. 概述 2. 反向代理服务器的工作原理 (1)作为内容服务器的替身 (2)作为内容服务器的负载均衡器 二. nginx反向代理实例 1.前期准备 2.代理服务器配置 3. ...
- 20190521 - macOS 中显示隐藏文件的快捷键
macOS 中显示隐藏文件,以前总是借助于命令行或第三方软件,其实有一个快捷键: shift + cmmand + .
- 写linux脚本你怎么能不知道位置参数!?
在写shell脚本的时候,我们经常会手动设置参数,然后对我们的输入的这些参数进行处理和分析,那么这个东东到底值怎么使用的呢? 1.$n $0代表命令本身,$1-9代表接受的第1-9个参数,10以上需要 ...
- PHP SQL注入
开发者容易遗漏的输入点: HTTP头 X-Forwarded-For 获取用户ip User-Agent 获取浏览器 Referer 获取之 ...
- 阿里P8架构师谈:Restful、SOAP、RPC、SOA、微服务之间的区别(转载)
转载来源:https://youzhixueyuan.com/the-difference-between-restful-soap-rpc-soa-and-micro-service.html 内容 ...
- eclipse控制台界面乱码问题
一.首先,eclipse的控制台必须用GBK编码,必须满足以下两个条件: 1.Windows---->Preferences---->Workspace---->Text file ...