1. Pause and resume a crawl

Scrapy supports this functionality out of the box by providing > the following facilities:

  • a scheduler that persists scheduled > >requests on disk

  • a duplicates filter that persists >visited requests on disk

  • an extension that keeps some spider state (key/value pairs) > persistent between > batches

run a crawl by

scrapy crawl somespider -s JOBDIR=crawls/somespider_dir

use Ctrl+C to close a drawl and resume by the same command above

2. 发起一次get请求

e.g.

页面A是新闻的列表,包含了每个新闻的链接

要发起一个请求去获取新闻的内容

通过设置request.meta,可以将参数带到callback函数中去,用response.meta接收

def parse(self, response):
newslist = response.xpath('//ul[@class="linkNews"]/li') for item in newslist:
news = News()
news['title'] = item.xpath('a/text()').extract_first(default = '') contentUri = item.xpath('a/@href').extract_first(default = '')
request = scrapy.Request(contentUri,
callback = self.getContent_callback,
headers = headers)
request.meta['item'] = news
yield request def getContent_callback(self, response):
news = response.meta['item']
item['content'] = response.xpath('//article[@class="art_box"]').xpath('string(.)').extract_first(default = '').strip()
yield item

3. 交互式shell

可以在这里交互式地获取各种信息,如response.status

我主要用来调试xpath(!shell中调试结果并不可靠)

PS C:\Users\patrick\Documents\Visual Studio 2017\Projects\ScrapyProjects> scrapy shell --nolog 'http://mil.news.sina.com.cn/2011-03-31/1342640379.html'
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x0000026EA72752B0>
[s] item {}
[s] request <GET http://mil.news.sina.com.cn/2011-03-31/1342640379.html>
[s] response <200 http://mil.news.sina.com.cn/2011-03-31/1342640379.html>
[s] settings <scrapy.settings.Settings object at 0x0000026EA8586940>
[s] spider <DefaultSpider 'default' at 0x26ea884bb38>
[s] Useful shortcuts:
[s] fetch(url[, redirect=True]) Fetch URL and update local objects (by default, redirects are followed)
[s] fetch(req) Fetch a scrapy.Request and update local objects
[s] shelp() Shell help (print this help)
[s] view(response) View response in a browser
In [1]: response.status
Out[1]: 200

在交互式环境里设置自定义headers

$ scrapy shell --nolog
...
...
>>> from scrapy import Request
>>> req = Request('douban.com', headers = {'User-Agent' : '...'})
>>> fetch(req)

if you just want to set user agent

scrapy shell -s USER_AGENT='useragent' 'https://movie.douban.com'

4. 命令行下向爬虫传参数

scrapy crawl myspider -a category=electronics

在爬虫中获取参数,直接通过参数名获取,如下面代码中的category

import scrapy

class MySpider(scrapy.Spider):
name = 'myspider' def __init__(self, category=None, *args, **kwargs):
super(MySpider, self).__init__(*args, **kwargs)
self.start_urls = ['http://www.example.com/categories/%s' % category]
# ...

5. 去除网页中的\r\n

用xpath中的normalize-space

以及extract_first是个好东西,还能加默认值

item['content'] = response.xpath('normalize-space(//div[@class="blkContainerSblkCon" and @id="artibody"])').extract_first(default = '')

6. 以编程方式停止一个爬虫

方法是抛出一个内置的异常CloseSpider

exception scrapy.exceptions.CloseSpider(reason='cancelled')

This exception can be raised from a spider callback to request the spider to be closed/stopped. Supported arguments:

Parameters: reason (str) – the reason for closing

def parse_page(self, response):
if 'Bandwidth exceeded' in response.body:
raise CloseSpider('bandwidth_exceeded')

7. [mysql] Incorrect string value: '\xF0\x9F\x8C\xB9' for column 'title' at row 1

连接数据库时的charset参数设置成utf8mb4

8. 写入文件时为utf-8编码而不是中文

在settings.py 文件末加上 FEED_EXPORT_ENCODING = 'utf-8'

9. soome things about Item

>>> import scrapy
>>> class A(scrapy.Item):
... post_id = scrapy.Field()
... user_id = scrapy.Field()
... content = scrapy.Field()
...
>>> type(A)
<class 'scrapy.item.ItemMeta'>

这里的post_iduser_id可以存储任何类型的数据

取数据的时候也可以像是操作dic一样

>>> a = A(post_id = '12312312', author_id = '2342_author_id')
>>> a['post_id']
'12312312'
>>> a['author_id']
'2342_author_id'

如果field未被赋值,直接用dic['key']的方法取数据会报'KeyError',解决办法是改用get方法

>>> a.get('content', default = 'empty')
'empty'
>>> a.get('content', 'empty')
'empty'

判断Item中是否存在某个field以及是否被赋值

>>> 'name' in a   # name是否被赋值
False
>>> 'name' in a.fields # a的属性里是否有 'name
False
>>> 'content' in a # content是否被赋值
False
>>> 'content' in a.fields
True

建议所有dic['key']都改成dic.get('key', '')

10. 日志写入到文件

settings.py中插入

LOG_STDOUT = True
LOG_FILE = 'scrapy_log.txt'

scrapy crawl MyCrawler -s LOG_FILE=/var/log/crawler_mycrawler.log

Reference

  1. Set headers for scrapy shell request

  2. Scrapy 1.5 documentation

[Scrapy] Some things about Scrapy的更多相关文章

  1. 从零安装Scrapy心得 | Install Python Scrapy from scratch

    1. 介绍 Scrapy,是基于python的网络爬虫框架,它能从网络上爬下来信息,是data获取的一个好方式.于是想安装下看看. 进到它的官网,安装的介绍页面 https://docs.scrapy ...

  2. Scrapy:学习笔记(2)——Scrapy项目

    Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...

  3. scrapy基础知识之 Scrapy 和 scrapy-redis的区别:

    Scrapy 和 scrapy-redis的区别 Scrapy 是一个通用的爬虫框架,但是不支持分布式,Scrapy-redis是为了更方便地实现Scrapy分布式爬取,而提供了一些以redis为基础 ...

  4. scrapy的安装,scrapy创建项目

    简要: scrapy的安装 # 1)pip install scrapy -i https://pypi.douban.com/simple(国内源) 一步到位 # 2) 报错1: building ...

  5. Scrapy:Python实现scrapy框架爬虫两个网址下载网页内容信息——Jason niu

    import scrapy class DmozSpider(scrapy.Spider): name ="dmoz" allowed_domains = ["dmoz. ...

  6. Scrapy基础(十四)————Scrapy实现知乎模拟登陆

    模拟登陆大体思路见此博文,本篇文章只是将登陆在scrapy中实现而已 之前介绍过通过requests的session 会话模拟登陆:必须是session,涉及到验证码和xsrf的写入cookie验证的 ...

  7. scrapy下载图片报[scrapy.downloadermiddlewares.robotstxt] DEBUG: Forbidden by robots.txt:错误

    本文转自:http://blog.csdn.net/zzk1995/article/details/51628205 先说结论,关闭scrapy自带的ROBOTSTXT_OBEY功能,在setting ...

  8. 【Scrapy】关于使用Scrapy框架爬虫遇到的问题1

    class testScrapy(scrapy.Spider): name = "testLogs" allowed_domains=["cnblogs.com" ...

  9. scrapy(一)scrapy 安装问题

    一.安装scrapy pip install scrapy 二.出现Microsoft Visual C++ 14.0相关问题 注:若出现以下安装错误 building 'twisted.test.r ...

随机推荐

  1. CF - 1117 F Crisp String

    题目传送门 题解: 枚举非法对. 如果 ‘a'  和 ’b' 不能相邻的话,那么删除 'a' 'b'之间的字符就是非法操作了. 假设题目给定的字符串为 "acdbe",所以删除cd ...

  2. codeforce 505 D. Mr. Kitayuta's Technology(tarjan+并查集)

    题目链接:http://codeforces.com/contest/505/problem/D 题解:先用tarjan缩点然后再用并查集注意下面这种情况 ‘ 这种情况只需要构成一个大环就行了,也就是 ...

  3. yzoj P1412 & 洛谷P1629 邮递员送信 题解

    有一个邮递员要送东西,邮局在结点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每次只能带一 ...

  4. 【转】Android CTS 测试

    http://blog.csdn.net/zxm317122667/article/details/8508013 Android-CTS 4.0.3测试基本配置 1. Download CTS CT ...

  5. 【LeetCode】BFS 总结

    BFS(广度优先搜索) 常用来解决最短路径问题. 第一次便利到目的节点时,所经过的路径是最短路径. 几个要点: 只能用来求解无权图的最短路径问题 队列:用来存储每一层便利得到的节点 标记:对于遍历过的 ...

  6. Wireshark解密HTTPS流量的两种方法

    原理 我们先回顾一下SSL/TLS的整个握手过程: Clienthello:发送客户端的功能和首选项给服务器,在连接建立后,当希望重协商.或者响应服务器的重协商请求时会发送. version:客户端支 ...

  7. Django-开放静态资源-获取请求携带的数据-pychram连接数据库-修改Django默认数据库-DjangoORM操作--表管理-记录管理-01

    目录 关于静态资源访问 为什么要配置静态文件才能获取静态资源 常见的静态文件种类 如何配置来开启访问权限 禁用浏览器缓存 django的自动重启机制(热启动) 静态文件接口动态解析 向服务器发送数据 ...

  8. 大数据平台搭建 - cdh5.11.1 - hue安装及集成其他组件

    一.简介 hue是一个开源的apache hadoop ui系统,由cloudear desktop演化而来,最后cloudera公司将其贡献给了apache基金会的hadoop社区,它基于pytho ...

  9. 简易数据分析 12 | Web Scraper 翻页——抓取分页器翻页的网页

    这是简易数据分析系列的第 12 篇文章. 前面几篇文章我们介绍了 Web Scraper 应对各种翻页的解决方法,比如说修改网页链接加载数据.点击"更多按钮"加载数据和下拉自动加载 ...

  10. 增删改查——DBUtils

    利用QueryRunner类实现对数据库的增删改查操作,需要先导入jar包:commons-dbutils-1.6.利用QueryRunner类可以实现对数据步骤的简化. 1.添加 运用JDBC工具类 ...