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的语法比较坑,但是你可以 ...
随机推荐
- django 2.1 配sql server 2008R2
请教了不少高手和度娘终于把这个事搞定了(基本上断断续续查试了2周时间),,,,,,,, 环境: 1-Microsoft Windows 2-Microsoft SQL SERVER2008 ...
- Quartz Scheduler misfireThreshold属性的意义与触发器超时后的处理策略
Quartz misfireThreshold属性的意义与触发器超时后的处理策略. 在配置quartz.properties有这么一个属性就是misfireThreshold,用来指定调度引擎设置触发 ...
- 模块_pip、os模块
一个python文件就是一个模块 1.标准模块 python自带的模块就是标准模块,也就是可以直接import进来的就是标准模块 import json import random import da ...
- ogg12.2中的新参数 AllowOutputDir
在一个测试中,通过普通的pump进程将数据写入远端主机,启动pump进程之后进程abended.查看进程日志,提示: 2018-04-07 13:26:21 ERROR OGG-25127 R ...
- latch release ......
MainControl_cfg.c brings error: not defined. /* e_TIMER_MCtrlLatchReleaseTime */ TIMER_ID_MCtr ...
- 02:golang基础
1.1 golang中的init函数和main函数 1.init函数和main函数 1. golang里面有两个保留的函数:init函数(用于所有package)和main函数(只能用于package ...
- Win32汇编学习(11):对话框(2)
我们将进一步学习对话框,探讨如何把对话框当成输入设备.如果您看了前一篇文章,那就会发现这次的例子只有少量的改动,就是把我们的对话框窗口附属到主窗口上.另外,我们还要学习通用对话框的用法. 理论: 把对 ...
- IP分为五类
IP地址分为五类: IP地址分为五类:A类保留给政府机构,B类分配给中等规模的公司,C类分配给任何需要的人,D类用于组播,E类用于实验. 常用的三类IP地址 IP = 网路地址(网络号)+主机地址(主 ...
- WebApi 运行原理
1.当请求过来时,首先经过Global 下面的Application_start()方法,在这个方法中注册了WebApiConfig.Register 2.WebApiConfig.Register把 ...
- 转 ObjExporter Unity3d导出场景地图寻路
http://wiki.unity3d.com/index.php?title=ObjExporter