- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我无法理解如何在我自己的从 CrawlSpider 继承的 Spider 中使用规则字段。我的蜘蛛正试图在旧金山的比萨饼黄页列表中爬行。
我试图让我的规则保持简单,只是为了看看蜘蛛是否会爬过响应中的任何链接,但我没有看到它发生。我唯一的结果是它产生对下一页的请求,然后产生对后续页面的请求。
我有两个问题:1. 蜘蛛收到响应后是否先处理规则再调用回调?或相反亦然?2.规则何时适用?
编辑:我想到了。我覆盖了 CrawlSpider 的解析方法。在查看该类中的 parse 方法后,我意识到这是它检查规则并抓取这些网站的地方。
注意:了解您要覆盖的内容
这是我的代码:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy import Selector
from yellowPages.items import YellowpagesItem
from scrapy.http import Request
class YellowPageSpider(CrawlSpider):
name = "yellowpages"
allowed_domains = ['www.yellowpages.com']
businesses = []
# start with one page
start_urls = ['http://www.yellowpages.com/san-francisco-ca/pizza?g=san%20francisco%2C%20ca&q=pizza']
rules = (Rule (SgmlLinkExtractor()
, callback="parse_items", follow= True),
)
base_url = 'http://www.yellowpages.com'
def parse(self, response):
yield Request(response.url, callback=self.parse_business_listings_page)
def parse_items(self, response):
print "PARSE ITEMS. Visiting %s" % response.url
return []
def parse_business_listings_page(self, response):
print "Visiting %s" % response.url
self.businesses.append(self.extract_businesses_from_response(response))
hxs = Selector(response)
li_tags = hxs.xpath('//*[@id="main-content"]/div[4]/div[5]/ul/li')
next_exist = False
# Check to see if there's a "Next". If there is, store the links.
# If not, return.
# This requires a linear search through the list of li_tags. Is there a faster way?
for li in li_tags:
li_text = li.xpath('.//a/text()').extract()
li_data_page = li.xpath('.//a/@data-page').extract()
# Note: sometimes li_text is an empty list so check to see if it is nonempty first
if (li_text and li_text[0] == 'Next'):
next_exist = True
next_page_num = li_data_page[0]
url = 'http://www.yellowpages.com/san-francisco-ca/pizza?g=san%20francisco%2C%20ca&q=pizza&page='+next_page_num
yield Request(url, callback=self.parse_business_listings_page)
最佳答案
所以对于你的两个问题..
在发出请求之前,会在发出请求之前处理爬虫规则...当然,如果响应不符合允许的域,理论上会收到响应,但只是被丢弃。
在发出请求之前再次使用爬虫规则。
在您的示例中,当您调用 parse() 方法时...虽然在您的情况下您使用它是正确的??!必须运行它来确认,但那些阅读的人除非您在 CRAWL 蜘蛛中明确覆盖 parse() 方法……当使用爬网蜘蛛时……蜘蛛中 pare 与爬虫的等价物是 parse_item()。 .. 爬虫中的 parse() 是它自己的逻辑函数...不应该在规则集中用作回调
关于python - 理解 Scrapy 的 CrawlSpider 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25459719/
我一直在学习如何使用 scrapy,尽管我一开始对 python 的经验很少。我开始学习如何使用 BaseSpider 进行抓取。现在我正在尝试抓取网站,但我遇到了一个让我很困惑的问题。这是来自官方网
我对 scrapy Crawlspider 有一个问题:基本上,如果引发 CloseSpider 异常,它不会像应该的那样退出。下面是代码: from scrapy.spiders import Cr
我正在尝试从此类别页面上给出的所有(#123)详细信息页面中抓取一些属性 - http://stinkybklyn.com/shop/cheese/但 scrapy 无法遵循我设置的链接模式,我也检查
我目前正在用 scrapy 编写一个爬虫。我想抓取网站上显示的所有文本,不是单个页面,而是所有子页面。我正在使用 CrawlSpider,因为我认为它也是为了抓取其他页面而设计的。这是我到目前为止编写
我正在开发一个 CrawlSpider ,它获取多个 Domain 对象(它包含 main_url 和 name -域名)。然后它会抓取整个页面的每个 Domain 对象。 因此有一个 Domain
对于我试图抓取的页面,我有时会在我的响应中返回一个“占位符”页面,其中包含一些自动重新加载的 javascript,直到它获得真正的页面。我可以检测到这种情况何时发生,并且我想重试下载和抓取页面。我在
我对规则在 CrawlSpider 中的评估顺序有疑问。如果我有以下代码: from scrapy.contrib.spiders.crawl import CrawlSpider, Rule fro
我正在尝试导航到每个县,然后从这里导航到每个县的每个城市: http://www.accountant-finder.com/CA/California-accountants.html 我的代码打开
Crawlspider 仅获取列表首页上匹配链接的子集。不久之后,它移动到第二页,成功跟踪所有匹配的链接,完全符合预期。如何让 Crawlspider 在进入第二页之前遵循所有匹配的链接? 我在第二条
我写了一个 scrapy crawlspider 来抓取一个结构类似于类别页面 > 类型页面 > 列表页面 > 项目页面的站点。在类别页面上有很多机器类别,每个类别都有一个包含很多类型的类型页面,每个
我是 Scrapy 的新手,我正在做一个抓取练习,我正在使用 CrawlSpider。虽然 Scrapy 框架工作得很好并且它遵循相关链接,但我似乎无法让 CrawlSpider 抓取第一个链接(主页
我创建的 CrawlSpider 没有正常工作。它解析第一页,然后停止而不继续到下一页。我做错了什么但无法检测到。希望有人给我一个提示,我应该做什么来纠正它。 “items.py”包括: from s
我在 Scrapy 中编写了一个蜘蛛,它基本上做得很好,并且完全按照它应该做的。 问题是我需要对它做一些小的改变,我尝试了几种方法都没有成功(例如修改 InitSpider)。这是脚本现在应该执行的操
我正在尝试创建一个实现 scrapy CrawlSpider 的 ExampleSpider。我的 ExampleSpider 应该能够处理仅包含艺术家信息的页面,仅包含专辑信息的页面,以及包含专辑和
问题陈述: 我在文件名为 myurls.csv 的每一行中都有一个论坛 url 列表,如下所示: https://www.drupal.org/user/3178461/track https://w
我想知道是否有一种方法可以在 scrapy 中的同一个蜘蛛中同时使用 Base 蜘蛛和 Crawl 蜘蛛! 假设我只想抓取 start_url 中提到的一个 url,然后对同一 start_url 中
我的蜘蛛有一些问题。我使用 splash 和 scrapy 来获取由 JavaScript 生成的“下一页”的链接。从第一页下载信息后,我想从后面的页面下载信息,但是LinkExtractor功能不能
最初的问题 我正在编写一个 CrawlSpider 类(使用 scrapy 库)并依赖大量的 scrapy 异步魔法 使其工作。在这里,精简了: class MySpider(CrawlSpider)
我一直在努力理解在网络抓取中使用 BaseSpider 和 CrawlSpider 的概念。我读过 docs.但是BaseSpider上没有提及。如果有人能解释一下 BaseSpider 和 Craw
我已经成功地使用 Scrapy 编写了一个非常简单的爬虫,具有这些给定的约束: 存储所有链接信息(例如: anchor 文本、页面标题),因此有 2 个回调 使用 CrawlSpider 来利用规则,
我是一名优秀的程序员,十分优秀!