2017-02-14 46 views
0

试图废下一页与Scrapy,Python的3.5使用urlib Python库使用的urllib与Scrapy为分页

import datetime 
    import urllib.request 
    import urllib.error 
    import urllib.parse 
    import socket 
    import scrapy 
    from scrapy.loader.processors import MapCompose, Join 
    from scrapy.loader import ItemLoader 
    from properties.items import PropertiesItem 


    class BasicSpider(scrapy.Spider): 
     name = "manual" 
     allowed_domains = ["web"] 

     # Start on the first index page 
     start_urls = (
      'http://scrapybook.s3.amazonaws.com/properties/index_00000.html', 
     ) 

     def parse(self, response): 
      # Get the next index URLs and yield Requests 
      next_selector = response.xpath('//*[contains(@class,"next")]//@href') 
      for url in next_selector.extract(): 
       yield Request(urllib.parse.urljoin(response.url, url)) 

      # Get item URLs and yield Requests 
      item_selector = response.xpath('//*[@itemprop="url"]/@href') 
      for url in item_selector.extract(): 
       yield Request(urllib.parse.urljoin(response.url, url), callback=self.parse_item) 


     def parse(self, response): 
      l = ItemLoader(item=PropertiesItem(), response=response) 
      l.add_xpath('title', '//*[@itemprop="name"]/text()') 
      return l.load_item() 

一切都工作得很好,没有错误,但Scrapy只读取第一页,但根据代码它应该获取所有接下来的页面

这里是输出

[{ 
    "title": [ 
     "bermondsey ec kennington drive acton seven rm", 
    ....... 
     "mary conversion borders eastham with gas" 
    }] 

// Only Page 0 Titles :(

什么不对的请求或urllib的调用语法?

PS:XPath时,Scrapy壳牌 'URL'

回答

1

让我们错用途Python包

  1. 开始使用请求没有将其导入,通过对其进行修复。

    从scrapy导入请求

  2. 错误使用从urllib的urljoin类,从的urllib.parse进口首次导入它

    urljoin

    目前使用urljoin直接不调用urllib.parse.urljoin

    改变它

    收率请求(urllib.parse.urljoin(response.url,URL)) 收率请求(urllib.parse.urljoin(response.url,URL),回调= self.parse_item)

  3. 不调用parse_item

    调用它

    高清解析(个体经营,响应):#replace解析到parse_item

PS:如果此代码,是学习Scrapy书那么这里是python3版本

https://github.com/Rahulsharma0810/Scrapy-Pagination-URLJOIN-Example

+0

哇完全GIT的例子!男人,你很棒,谢谢你。 –

0

你似乎有两个parse功能。所以你只有第二个,因为它覆盖了第一个。

只需将第二个重命名为parse_item,就像您代码的其余部分似乎表明的那样。