【问题标题】:Scrapy Spider not following Request callbackScrapy Spider 不遵循请求回调
【发布时间】:2015-02-19 18:49:11
【问题描述】:

我已经阅读了Scrapy: Follow link to get additional Item data? 并关注了它,但它不起作用,可能是一个简单的错误,所以我把我的蜘蛛的源代码。

import scrapy
from scrapy.spider import Spider
from scrapy.selector import Selector

class MySpider1(Spider):
    name = "timeanddate"
    allowed_domains = ["http://www.timeanddate.com"]
    start_urls = (
        'http://www.timeanddate.com/holidays/',
    )

    def parse(self, response):
        countries = Selector(response).xpath('//div[@class="fixed"]//li/a[contains(@href, "/holidays/")]')

        for item in countries:

            link = item.xpath('@href').extract()[0]
            country = item.xpath('text()').extract()[0]

            linkToFollow = self.allowed_domains[0] + link + "/#!hol=1"

            print link  # link
            print country  # text in a HTML tag
            print linkToFollow

            request = scrapy.Request(linkToFollow, callback=self.parse_page2)


    def parse_page2(self, response):
        print "XXXXXX"
        hxs = HtmlXPathSelector(response)

        print hxs

我也在尝试获取每个国家/地区的所有假期列表,这就是我需要进入另一个页面的内容。

我不明白为什么不调用 parse_page2。

【问题讨论】:

  • 你永远不会返回parse中的请求
  • 我累了退货,但它也无法正常工作。如果你有时间,请用代码告诉我你的想法。
  • yield request 在request = scrapy.Request(...) 行之后。
  • 我也试过了,没用。

标签: python scrapy


【解决方案1】:

我可以使用 Link Extractors 使您的示例工作

这是一个例子:

#-*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider,Rule
from scrapy.contrib.linkextractors.lxmlhtml import LxmlLinkExtractor

class TimeAndDateSpider(CrawlSpider):
    name = "timeanddate"
    allowed_domains = ["timeanddate.com"]
    start_urls = [
        "http://www.timeanddate.com/holidays/",
    ]


    rules = (
            Rule (LxmlLinkExtractor(restrict_xpaths=('//div[@class="fixed"]//li/a[contains(@href, "/holidays/")]',))
                , callback='second_page'),
            ) 

    #2nd page
    def second_page(self,response):
        print "second page - %s" % response.url

将继续尝试使请求回调示例正常工作

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多