scrapy框架初级
pip install C:\python\Anaconda3\Twisted-18.7.0-cp36-cp36m-win_amd64.whl
pip install scrapy
scrapy startproject projectname
命令部分 文件名 爬取得网站
scrapy genspider baidu baidu.com
scrapy genspider -t crawl baidu baidu.com
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.3.6000' # Obey robots.txt rules
ROBOTSTXT_OBEY = False
DOWNLOAD_DELAY = 3
ITEM_PIPELINES = {
'xiaoshuo_pc.pipelines.XiaoshuoPcPipeline': 300,
}
scrapy crawl name(变量值)
scrapy crawl name -o book.json(输出到文件{json、xml、csv})
scrapy crawl name -o book.json -t json(-t 代表格式输出,一般忽略)
**第一次运行的时候,我遇到no module named win32API错误,这是因为Python没有自带访问windows系统API的库的,需要下载第三方库。库的名称叫pywin32,可以从网上直接下载,下载链接:http://sourceforge.net/projects/pywin32/files%2Fpywin32/ (下载适合你的Python版本)下载后放置到scripts目录下双机运行,即可((或者pip install pypiwin32))
from scrapy.cmdline import execute
execute("scrapy crawl zol".split()) # zol为zol文件中的变量定义的名
class ShiqikSpider(scrapy.Spider):
name = 'shiqik'
allowed_domains = ['17k.com']
start_urls = ['https://www.81zw.us/book/1379/6970209.html'] def parse(self, response):
title=response.xpath('//div[@class="bookname"]/h1/text()').extract_first()
content=''.join(response.xpath('//div[@id="content"]/text()').extract()).replace(' ','\n')
yield {"title":title,"content":content}
next_page=response.xpath('//div[@class="bottem2"]/a[3]/@href').extract_first()
if next_page.find(".html")!=-1:
print("继续下一个url")
new_url=response.urljoin(next_page)
yield scrapy.Request(new_url,callback=self.parse,dont_filter=True)
class BayizhongwenSpider(CrawlSpider):
name = 'bayizhongwen'
allowed_domains = ['81zw.us']
# start_urls = ['https://www.81zw.us/book/1215/863759.html']
start_urls = ['https://www.81zw.us/book/1215'] rules = (
Rule(LinkExtractor(restrict_xpaths=r'//dl/dd[2]/a'), callback='parse_item', follow=True),
Rule(LinkExtractor(restrict_xpaths=r'//div[@class="bottem1"]/a[3]'), callback='parse_item', follow=True),
)
def parse_item(self, response):
title=response.xpath('//div[@class="bookname"]/h1/text()').extract_first()
content=''.join(response.xpath('//div[@id="content"]/text()').extract()).replace(' ','\n')
print({"title":title,"content":content})
yield {"title":title,"content":content}
(venv) C:\Users\noc\PycharmProjects>scrapy startproject tupian
(venv) C:\Users\noc\PycharmProjects\tupian>scrapy genspider zol zol.com.cn
# Crawl responsibly by identifying yourself (and your website) on the user-agent
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' # Obey robots.txt rules
ROBOTSTXT_OBEY = False
DOWNLOAD_DELAY = 3 # Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
# 'tupian.pipelines.TupianPipeline': 300,
'scrapy.contrib.pipeline.images.ImagesPipeline': 300,
}
# 增加图片存放目录
IMAGES_STORE='e:/img'
from scrapy.cmdline import execute
execute("scrapy crawl zol".split()) # zol为zol文件中的变量定义的名
五、主文件代码:
import scrapy class ZolSpider(scrapy.Spider):
name = 'zol'
allowed_domains = ['zol.com.cn']
start_urls = ['http://desk.zol.com.cn/bizhi/7239_89590_2.html'] # 爬取图片页面的地址 def parse(self, response):
image_url = response.xpath('//img[@id="bigImg"]/@src').extract() # 爬取第一张图片的地址
image_name = response.xpath('string(//h3)').extract_first() # 爬取图片名称
yield {"image_url": image_url, "image_name": image_name} # 推送
next_page = response.xpath('//a[@id="pageNext"]/@href').extract_first() # 爬取图片下一张按钮的地址
if next_page.find('.html') != -1: # 判断最后一张图片地址如果不包含.html
yield scrapy.Request(response.urljoin(next_page), callback=self.parse)
六、middlewares文件
from tupian.settings import USER_AGENT
from random import choice
from fake_useragent import UserAgent # User-Agent设置
class UserAgentDownloaderMiddleware(object):
def process_request(self, request, spider):
# if self.user_agent:
# request.headers.setdefault(b'User-Agent',choice(USER_AGENT))
request.headers.setdefault(b'User-Agent', UserAgent().random) # 代理设置
class ProxyMiddleware(object):
def process_request(self, request, spider):
# request.meta['proxy']='http://ip:port'
request.meta['proxy']='http://124.235.145.79:80'
# request.meta['proxy']='http://user:passwd@ip:port'
# request.meta['proxy']='http://398707160:j8inhg2g@139.224.116.10:16816'
scrapy框架初级的更多相关文章
- Python爬虫进阶三之Scrapy框架安装配置
初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此整理如下. Windows 平台: 我的系统是 ...
- Python爬虫进阶之Scrapy框架安装配置
Python爬虫进阶之Scrapy框架安装配置 初级的爬虫我们利用urllib和urllib2库以及正则表达式就可以完成了,不过还有更加强大的工具,爬虫框架Scrapy,这安装过程也是煞费苦心哪,在此 ...
- Python爬虫Scrapy框架入门(2)
本文是跟着大神博客,尝试从网站上爬一堆东西,一堆你懂得的东西 附上原创链接: http://www.cnblogs.com/qiyeboy/p/5428240.html 基本思路是,查看网页元素,填写 ...
- Python爬虫Scrapy框架入门(1)
也许是很少接触python的原因,我觉得是Scrapy框架和以往Java框架很不一样:它真的是个框架. 从表层来看,与Java框架引入jar包.配置xml或.property文件不同,Scrapy的模 ...
- Scrapy框架使用—quotesbot 项目(学习记录一)
一.Scrapy框架的安装及相关理论知识的学习可以参考:http://www.yiibai.com/scrapy/scrapy_environment.html 二.重点记录我学习使用scrapy框架 ...
- Python爬虫从入门到放弃(十一)之 Scrapy框架整体的一个了解
这里是通过爬取伯乐在线的全部文章为例子,让自己先对scrapy进行一个整理的理解 该例子中的详细代码会放到我的github地址:https://github.com/pythonsite/spider ...
- Python爬虫从入门到放弃(十二)之 Scrapy框架的架构和原理
这一篇文章主要是为了对scrapy框架的工作流程以及各个组件功能的介绍 Scrapy目前已经可以很好的在python3上运行Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是 ...
- python爬虫scrapy框架——人工识别登录知乎倒立文字验证码和数字英文验证码(2)
操作环境:python3 在上一文中python爬虫scrapy框架--人工识别知乎登录知乎倒立文字验证码和数字英文验证码(1)我们已经介绍了用Requests库来登录知乎,本文如果看不懂可以先看之前 ...
- 一个scrapy框架的爬虫(爬取京东图书)
我们的这个爬虫设计来爬取京东图书(jd.com). scrapy框架相信大家比较了解了.里面有很多复杂的机制,超出本文的范围. 1.爬虫spider tips: 1.xpath的语法比较坑,但是你可以 ...
随机推荐
- 蓝桥杯2018 B组 第九题
#include <bits/stdc++.h> #define M 1005 using namespace std; int n; char map1[M][M]; bool vis[ ...
- Kubernetes外挂配置管理—ConfigMap介绍
目录贴:Kubernetes学习系列 其他容器编排调度工具会大谈特谈“轻应用”.“十二要素应用”,这样就势必会对企业级复杂应用做很大的改动.Kubernetes是为了解决“如何合理使用容器支撑企业级复 ...
- mysql 热备份
xtrabackup mysql 的备份和恢复 1.准备 安装依赖 yum install perl-DBD-MySQL perl-Time-HiRes libaio libaio-devel -y ...
- 5.Dubbo原理解析-代理之Javassist字节码技术生成代理 (转)
转载自 斩秋的专栏 http://blog.csdn.net/quhongwei_zhanqiu/article/details/41597219 JavassistProxyFactory:利用 ...
- opencv学习之路(20)、直方图应用
一.直方图均衡化--equalizeHist() #include "opencv2/opencv.hpp" using namespace cv; void main() { 6 ...
- 再谈git和github-深入理解-2
github中的 sloc是什么意思? sloc: source lines of code: 代码行数, 源代码行. 要向仓库中 create file/upload file/edit file等 ...
- Deep Dream 模型
本节的代码参考了TensorFlow 源码中的示例程序https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/ ...
- centos7安装node
centos7安装node 二进制文件安装 node=v10.13.0 file=node-${node}-linux-x64 wget https://nodejs.org/dist/${node} ...
- js 数组元素排序
字母排序 <html> <body> <script type="text/javascript"> ) arr[] = "Georg ...
- 一起用ipython
安装 安装python2版本的软件包就用命令 pip install ipython 安装python3版本对应的软件包就用命令 pip3 install ipython 进入了ipython,ipy ...