2015-05-04 26 views
2

我在Windows 64位機器上的Python 2.7.9上使用Scrapy 0.24。我試圖告訴scrapy從一個特定的URL http://www.allen-heath.com/products/開始,並從那裏僅收集來自網址的數據,其中url包含字符串ahproducts如何抓取網站並僅解析使用Scrapy匹配RegEx的頁面0.24

不幸的是,當我這樣做時,根本沒有任何數據被刮掉。我究竟做錯了什麼?以下是我的代碼如下。如果我可以提供更多信息來幫助解答問題,請詢問我將進行修改。

這是我的履帶日誌的pastebin:http://pastebin.com/C2QC23m3

謝謝。

import scrapy 
import urlparse 

from allenheath.items import ProductItem 
from scrapy.selector import Selector 
from scrapy.http import HtmlResponse 
from scrapy.contrib.spiders import Rule 
from scrapy.contrib.linkextractors import LinkExtractor 

class productsSpider(scrapy.Spider): 
    name = "products" 
    allowed_domains = ["http://www.allen-heath.com/"] 
    start_urls = [ 
     "http://www.allen-heath.com/products/" 
    ] 
    rules = [Rule(LinkExtractor(allow=['ahproducts']), 'parse')] 

    def parse(self, response): 
     for sel in response.xpath('/html'): 
      item = ProductItem() 
      item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['itemcode'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract() 
      item['desc'] = sel.css('#tab1 #productcontent').extract() 
      item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract() 
      item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['image_urls'] = sel.css('#tab1 #productcontent .col-sm-9 img').xpath('./@src').extract() 
      item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']] 
      yield item 

從eLRuLL的一些建議這裏是我更新的蜘蛛文件。我修改了start_url以包含一個包含「ahproducts」鏈接的頁面。我的原始代碼在起始頁上沒有任何匹配的網址。

products.py

import scrapy 
import urlparse 

from allenheath.items import ProductItem 
from scrapy.selector import Selector 
from scrapy.http import HtmlResponse 
from scrapy.contrib.spiders import Rule 
from scrapy.contrib.linkextractors import LinkExtractor 

class productsSpider(scrapy.contrib.spiders.CrawlSpider): 
    name = "products" 
    allowed_domains = ["http://www.allen-heath.com/"] 
    start_urls = [ 
     "http://www.allen-heath.com/key-series/ilive-series/ilive-remote-controllers/" 
    ] 
    rules = (
      Rule(
       LinkExtractor(allow='.*ahproducts.*'), 
       callback='parse_item' 
       ), 
      ) 

    def parse_item(self, response): 
     for sel in response.xpath('/html'): 
      item = ProductItem() 
      item['model'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['itemcode'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['shortdesc'] = sel.css('#prodsingleouter > div > div > h3::text').extract() 
      item['desc'] = sel.css('#tab1 #productcontent').extract() 
      item['series'] = sel.css('#pagestrip > div > div > a:nth-child(3)::text').extract() 
      item['imageorig'] = sel.css('#prodsingleouter > div > div > h2::text').extract() 
      item['image_urls'] = sel.css('#tab1 #productcontent .col-sm-9 img').xpath('./@src').extract() 
      item['image_urls'] = [urlparse.urljoin(response.url, url) for url in item['image_urls']] 
      yield item 

回答

2

首先,使用規則,你需要使用scrapy.contrib.spiders.CrawlSpiderscrapy.Spider

然後,你的方法的名稱更改爲類似parse_itemparse並更新你的規則,如:

rules = (
     Rule(
      LinkExtractor(allow='.*ahproducts.*'), 
      callback='parse_item' 
     ), 
    ) 

parse方法總是叫作爲start_urls請求的響應。

最後只能改變allowed_domainsallowed_domains = ["allen-heath.com"]

P.D.抓取不同級別規則的網站,你需要指定其鏈接到跟隨並鏈接到解析,這樣的事情:

rules = (
    Rule(
     LinkExtractor(
      allow=('some link to follow') 
     ), 
     follow=True, 
    ), 
    Rule(
     LinkExtractor(
      allow=('some link to parse') 
     ), 
     callback='parse_method', 
    ), 
) 
+0

匹配'ahproducts'都對啓動網頁鏈接,該帶網址的網頁用'http:// www.allen-heath.com/series /'或'/ key-series /'。您只能從'http:// www.allen-heath.com/products /'開始獲得頁面。我錯誤地認爲scrapy會遵循'http:// www.allen-heath.com/products /'上的鏈接,直到找到匹配'ahproducts'的頁面。因此,如果我理解正確,使用規則,scrapy將只匹配並收集符合我的規則並存在於我的'start_url'上的鏈接數據。在這種情況下,我想我需要首先生成許多'start_url'? – jkupczak

+0

根據您的評論,我已經提前將類產品Spider(scrapy.Spider)'改爲'class productsSpider(scrapy.contrib.spiders.CrawlSpider):'並且我使用了一個start_url,它與'ahproducts '在裏面。但它仍然沒有提取任何數據。 – jkupczak

+0

請更新您的問題,並更改 – eLRuLL