scrapy

Capture scrapy spider running status using an already defined decorator

假如想象 提交于 2020-05-17 10:09:21
问题 So I have a custom decorator called task that captures the status of a function. e.g., @task(task_name='tutorial', alert_name='tutorial') def start(): raw_data = download_data() data = parse(raw_data) push_to_db(data) if if __name__ == "__main__": start() So here the task decorator monitors the status of start function and send the error message to a central monitor system using alert_name if it fails otherwise sends successful message if it succeeds. Now I want to add this decorator to

Capture scrapy spider running status using an already defined decorator

我与影子孤独终老i 提交于 2020-05-17 10:06:47
问题 So I have a custom decorator called task that captures the status of a function. e.g., @task(task_name='tutorial', alert_name='tutorial') def start(): raw_data = download_data() data = parse(raw_data) push_to_db(data) if if __name__ == "__main__": start() So here the task decorator monitors the status of start function and send the error message to a central monitor system using alert_name if it fails otherwise sends successful message if it succeeds. Now I want to add this decorator to

How to perform scrapy XHR form request using Scrapy Python

空扰寡人 提交于 2020-05-15 09:30:13
问题 First of all my script works when login is performed using for the request to send data via HTTP POST. But it doesn't work with ajax form submission. from scrapy.http import Request, FormRequest from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Rule # from scrapy.selector import HtmlXPathSelector from scrapy.http import FormRequest import scrapy from scrapy.crawler import CrawlerProcess from scrapy.utils.response import open_in_browser from bs4 import

How to change request url before making request in scrapy?

荒凉一梦 提交于 2020-05-14 19:56:07
问题 I need to modify my request url before a response is downloaded. But I'm not able to change it. Even after modifying the request url using request.replace(url=new_url) , the process_response prints the non-modified url. Here's the code of the middleware: def process_request(self, request, spider): original_url = request.url new_url= original_url + "hello%20world" print request.url # This prints the original request url request=request.replace(url=new_url) print request.url # This prints the

999 response when trying to crawl LinkedIn with Scrapy

笑着哭i 提交于 2020-05-11 03:24:49
问题 I am trying the Scrapy framework to extract some information from LinkedIn. I am aware that they are very strict with people trying to crawl their website, so I tried a different user agent in my settings.py. I also specified a high download delay but it still seems to block me right off the bat. USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0' ROBOTSTXT_OBEY = False DOWNLOAD_DELAY = 2 REDIRECT_ENABLED = False RETRY_ENABLED = False DEPTH_LIMIT

手把手教你进行Scrapy中item类的实例化操作

筅森魡賤 提交于 2020-05-09 14:48:32
接下来我们将在爬虫主体文件中对Item的值进行填充。 1、首先在爬虫主体文件中将Item模块导入进来,如下图所示。 2、第一步的意思是说将items.py中的ArticleItem类导入到爬虫主体文件中去,将两个文件串联起来,其中items.py的部分内容如下图所示。 3、将这个ArticleItem类导入之后,接下来我们就可以对这个类进行初始化,并对其进行相应值的填充。首先去parse_detail函数下对其进行实例化,实例化的方法也十分简单,如下图所示。 4、接下来,我们将填充对应的值。实际上我们在之前通过Xpath或者CSS选择器已经获取到了目标数据,如下图所示,现在要做的就是依次填充目标字段的值。 5、我们可以像字典一样来给目标字段传值,例如item[“title”]= title,其他的目标字段的填充也是形如该格式,填充完成之后如下图所示。 其中,目标字段可以参考items.py中定义的item,这样可以加快填充的速度。 6、到这里,我们已经将需要填充的字段全部填充完成了,之后我们需要调用yield,这点十分重要。再调用yield之后,实例化后的item就会自动传递到pipeline当中去。可以看到下图中的pipelines.py中默认给出的代码,说明pipeline其实是可以接收item的。 7、到这里,关于实例化item的步骤就已经完成了,是不是比较简单呢

SpiderMan成长记(爬虫之路)

偶尔善良 提交于 2020-05-08 03:46:21
第一章 爬虫基础 1.1 爬虫基本原理 1.2 请求库 -- urllib库的使用 1.3 请求库 -- requests库的使用 1.4 数据解析 -- 正则基础  1.5 数据解析 -- lxml与Xpath 1.6 数据解析 -- BeautifulSoup库详解  1.6 动态数据抓取 -- PyQuery详解  1.7 动态数据抓取 -- Selenium详解 第二章 爬虫实战 2.1 Requests + 正则抓取猫眼电影 2.2 分析Ajax请求爬取今日头条街拍美图 2.3 使用Selenium模拟浏览器抓取淘宝视频美食信息 2.4 第三章 框架 -- scrapy   3.1 scrapy框架 -- 安装与基本使用   3.1 scrapy框架 -- Spider详解   3.3 scrapy框架 -- 选择器用法   3.4 scrapy框架 -- Item Pipeline   3.5 scrapy框架 -- DownloadMiddleware 来源: oschina 链接: https://my.oschina.net/u/4311773/blog/4249267

使用PyMongo访问需要认证的MongoDB

我的未来我决定 提交于 2020-05-08 00:12:32
Windows 10家庭中文版,Python 3.6.4,PyMongo 3.7.0,MongoDB 3.6.3,Scrapy 1.5.0, 前言 在Python中,使用PyMongo访问MongodB,作者Mike Dirolf,维护人员Bernie Hackett <bernie@mongodb.com>,相关链接如下: - PyPI官网 - GitHub官网 - 最新版本3.7.0文档 说明,关于文档,可以从GitHub下载PyMongo(需要安装sphinx先),然后自行编译文档。 说明,PyMongo还有一些附属包,以提供与MongoDB服务器匹配的功能,比如TLS / SSL、GSSAPI、srv、wire protocol compression with snappy等,大家可以根据需要安装。 本文介绍使用PyMongo访问需要认证的MongoDB,包括从IDLE、Scrapy爬虫程序来访问。 参考官文: PyMongo Authentication Examples (可以直接阅读官文,忽略本文剩余部分,) 本地MongoDB服务器介绍 打开MongoDB服务器: mongod --dbpath d:\p\mdb2dir --logpath d:\p\mdb2dir\log --logappend --auth --directoryperdb

手把手教你使用Python爬取西刺代理数据(下篇)

自闭症网瘾萝莉.ら 提交于 2020-05-06 09:19:42
/1 前言/ 前几天小编发布了 手把手教你使用Python爬取西次代理数据(上篇) ,木有赶上车的小伙伴,可以戳进去看看。今天小编带大家进行网页结构的分析以及网页数据的提取,具体步骤如下。 /2 首页分析及提取/ 首先进入网站主页,如下图所示。 简单分析下页面,其中后面的 1 是页码的意思,分析后发现每一页有100 多条数据,然后网站底部总共有 2700+页 的链接,所以总共ip 代理加起来超过 27 万条数据,但是后面的数据大部分都是很多年前的数据了,比如 2012 年,大概就前 5000 多条是最近一个月的,所以决定爬取前面100 页。通 过网站 url 分析,可以知道这 100 页的 url 为: 规律显而易见,在程序中,我们使用一个 for 循环即可完整这个操作: 其中 scrapy 函数是爬取的主要逻辑,对应的代码为: 通过这个方式,我们可以得到每一页的数据。 /3 网页元素分析及提取/ 接下来就是对页面内的元素进行分析,提取其中的代理信息。 如上图,我们目的是进行代理地域分布分析,同时,在爬取过程中需要使用爬取的数据进行代 理更新,所以需要以下几个字段的信息: Ip 地址、端口、服务器位置、类型 为此,先构建一个类,用于保存这些信息: 这样,每爬取一条信息,只要实例化一个 ProxyBean 类即可,非常方便。 接下来就是提取元素过程了,在这个过程我使用了正则表达式和

笔记-爬虫-scrapy-srcapy-redis组件

纵然是瞬间 提交于 2020-05-06 03:36:02
笔记-爬虫-scrapy-srcapy-redis组件 1. 简介 scrapy是一个爬虫框架,但不支持分布式,scrapy-redis是为了更方便的实现scrapy分布式爬虫的组件。 可以在pypi上找到:https://pypi.org/project/scrapy-redis/ 1.1. 安装 可以使用pip安装 pip install scrapy-redis pip show scrapy-redis 目前最新版是0.6.8。 2. 使用 Scrapy-redis提供了下面四种组件(components):(意味着原始scrapy爬虫这四个部分都要做相应的修改) Scheduler Duplication Filter Item Pipeline Base Spider 先不管那么多,先跑一个案例; -----实测------ 3. scrapy-redis实测 3.1. 环境准备 主:虚拟机 centos6.5 从:物理机win8 3.2. redis安装配置 见笔记-redis安装 3.3. centos python环境安装 centos下已有python环境,安装参考文档:笔记-python3环境安装-centos6.5 安装相关包: pip3 install redis,scrapy,scrapy-redis,lxml 包括redis-py,scrapy