scrapy

Scrapy CrawlSpider rules with multiple callbacks

我们两清 提交于 2020-01-13 06:00:53
问题 I'm tring to create an ExampleSpider which implements scrapy CrawlSpider. My ExampleSpider should be able to process pages containing only artist info, pages containing only album info, and some other pages which contains both album and artist info. I was able to handle First two scenarios. but the problem occurs in third scenario. I'm using parse_artist(response) method to process artist data, parse_album(response) method to process album data. My question is, If a page contains both artist

Scrapy and Xpath to extract data from javascript code

丶灬走出姿态 提交于 2020-01-13 05:57:08
问题 I am in the process of learning and simultaneously building a web spider using scrapy. I need help with extracting some information from the following javascript code: <script language="JavaScript" type="text/javascript+gk-onload"> SKART = (SKART) ? SKART : {}; SKART.analytics = SKART.analytics || {}; SKART.analytics["category"] = "television"; SKART.analytics["vertical"] = "television"; SKART.analytics["supercategory"] = "homeentertainmentlarge"; SKART.analytics["subcategory"] = "television"

How to get stats from a scrapy run?

痴心易碎 提交于 2020-01-13 05:50:14
问题 I am running the scrapy spider from external file as per the example in scrapy docs. I want to grab the stats provided by the Core API and store it to mysql table after the crawl is finished. from twisted.internet import reactor from scrapy.crawler import Crawler from scrapy import log, signals from test.spiders.myspider import * from scrapy.utils.project import get_project_settings from test.pipelines import MySQLStorePipeline import datetime spider = MySpider() def run_spider(spider):

爬虫---快速认识scrapy

半世苍凉 提交于 2020-01-12 17:40:42
1.写一个爬虫,需要做很多事情。比如:发送网络请求,数据解析,数据存储,反扒虫虫机制(更换IP代理,设置请求头等),异步请求等。这些工作如果每次都要从零开始写的话,比较浪费时间。因此scrapy吧一些基础的东西封装好了,在他上面写爬虫可以变的更加高效(爬去效率和开发效率)。因此真正在公司里,一些上了量的爬虫,都是使用scrapy框架来解决。 2.scrapy框架模块功能:Scrapy Engine(引擎):Scrapy框架的核心部分。负责在Spider和ItemPipeline,Downloader,Scheduer中间通信,传递数据。 3.Spider(爬虫):发送需要爬取的链接给引擎,最后引擎吧其他模块请求回来的数据在发送给爬虫,爬虫就去解析想要的数据。这个部分是我们开发者自己写的,因为要爬取哪些链接,页面中的哪些数据是我们需要的,都是由程序员自己决定。 4.Scheduler(调度器):负责接收引擎发送过来的请求,并按照移动的方式进行排列和整理,负责调度请求的 顺序等。 5。Downloader(下载器):负责接收引擎传过来的下载请求,然后去网络上下载对应的数据在交还给引擎。 6.Item Pipeline(管道):负责将Spider(爬虫)传递过来的数据进行保存。具体保存在哪里,应该看开发者自己的需求 7.Downloader Middlewares(下载中间件)

Scrapy初始

守給你的承諾、 提交于 2020-01-12 16:15:12
Scrapy框架(爬虫框架) 一、什么是Scrapy?   Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍。   所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化存储等)的具有很强通用性的项目模板。   对于框架的学习,重点是要学习其框架的特性、各个功能的用法即可。 二、安装scrapy   1、pip3 install wheel 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted 进入下载目录,执行 pip3 install Twisted-19.2.1-cp36-cp36m-win_amd64.whl pip3 install pywin32   5、pip3 install scrapy scrapy的使用方法   1、创建项目: scrapy startproject xxx   2、创建爬虫文件: scrapy genspider first www.xxx.com   3、执行爬虫文件:scrapy crawl first   4、执行爬虫文件不打印日志: scrapy crawl budejie --nolog   5、执行爬虫文件并持久化存储到csv文件中: scrapy crawl budejie -o budejie

Scrapy CrawlSpider + Splash: how to follow links through linkextractor?

青春壹個敷衍的年華 提交于 2020-01-12 07:42:04
问题 I have the following code that is partially working, class ThreadSpider(CrawlSpider): name = 'thread' allowed_domains = ['bbs.example.com'] start_urls = ['http://bbs.example.com/diy'] rules = ( Rule(LinkExtractor( allow=(), restrict_xpaths=("//a[contains(text(), 'Next Page')]") ), callback='parse_item', process_request='start_requests', follow=True), ) def start_requests(self): for url in self.start_urls: yield SplashRequest(url, self.parse_item, args={'wait': 0.5}) def parse_item(self,

How to stop scrapy spider after certain number of requests?

一笑奈何 提交于 2020-01-12 03:56:46
问题 I am developing an simple scraper to get 9 gag posts and its images but due to some technical difficulties iam unable to stop the scraper and it keeps on scraping which i dont want.I want to increase the counter value and stop after 100 posts. But the 9gag page was designed in a fashion in each response it gives only 10 posts and after each iteration my counter value resets to 10 in this case my loop runs infintely long and never stops. # -*- coding: utf-8 -*- import scrapy from _9gag.items

How to stop scrapy spider after certain number of requests?

不羁岁月 提交于 2020-01-12 03:56:11
问题 I am developing an simple scraper to get 9 gag posts and its images but due to some technical difficulties iam unable to stop the scraper and it keeps on scraping which i dont want.I want to increase the counter value and stop after 100 posts. But the 9gag page was designed in a fashion in each response it gives only 10 posts and after each iteration my counter value resets to 10 in this case my loop runs infintely long and never stops. # -*- coding: utf-8 -*- import scrapy from _9gag.items

Python爬虫5.3 — scrapy框架spider[Request和Response]模块的使用

杀马特。学长 韩版系。学妹 提交于 2020-01-12 02:38:37
Python爬虫5.3 — scrapy框架spider[Request和Response]模块的使用 综述 Request对象 scrapy.Request()函数讲解: Response对象 发送POST请求 模拟登陆 模拟登陆人人网 其他博文链接 综述 本系列文档用于对Python爬虫技术的学习进行简单的教程讲解,巩固自己技术知识的同时,万一一不小心又正好对你有用那就更好了。 Python 版本是3.7.4 我们在前面学习reuqests库的时候是如何进行翻页请求的?首先找到下一页地址,然后再使用 requests.get(next_url) 进行请求获取。那么我们在Scrapy框架中如何对下页进行构造请求的呢?本篇讲解如何构造请求模块。 Request对象 在第一篇入门文章中我们已经在爬取糗百实例中实现了下一页请求获得的功能在爬虫中增加下面代码即可: # 获取下一页地址 next_url = response . xpath ( '//ul[@class="pagination"]/li[last()]/a/@href' ) . get ( ) if not next_url : # 没有下一页地址结束爬虫 return else : # 将下一页请求返回给调度器 yield scrapy . Request ( self . base_url + next_url ,

scrapy框架结合项目使用,超级合适新手

回眸只為那壹抹淺笑 提交于 2020-01-12 02:15:07
一、流程步骤 HelloWorld Scrapy 创建一个工程 scrapy startproject XXX 创建一个爬虫 scrapy genspider YYY domain domain 爬取主站地址 运行爬虫 scrapy crawl YYY 完善爬虫 定向获取内容 parse函数 参数 response response xpath 写规则就可以 会返回提取好的内容 Selector get 获取内容 extract extract_all re css 二、代码操作: 1下载: pip install scrapy 2.终端创建项目ZhouWu scrapy startproject ZhouWu 3.pycharm打开项目,配置虚拟环境,生成爬虫文件;爬取http://lab.scrapyd.cn/这个网站,执行命令后会生成lab.py文件; scrapy genspider lab lab.scrapyd.cn 4.运行此蜘蛛文件 scrapy crawl lab 5.scrapy项目架构原理解析: 爬取流程:在Spiders中编写爬虫,把开始的地址配置好,会交给Scheduler调度器,Scheduler从请求队列中拿出调度器,把Requests发出去,Requests对应互联网资源,给Downloader下载器把资源变成Response,回到Spiders中