2016-11-26 6 views
0

問題は、「start_urls」に直接商品URLを追加するとすべて正常に機能することです。しかし、製品ページをクロール(すべてのページに戻り「200」をクロール)それはこすっていません.... 中に表示されたとき、私はクモを通じ実行している:治療のクロールはこすりません

scrape crawl site_products -t csv -o Site.csv 

スパイダーコード:

#-*- coding: utf-8 -*- 
from scrapy.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor 
from site.items import SiteItem 
import datetime 


class SiteProducts(CrawlSpider): 
    name = 'site_products' 
    allowed_domains = ['www.example.com'] 
    start_urls = [ 
     #'http://www.example.com/us/sweater_cod39636734fs.html', 
     #'http://www.example.com/us/sweater_cod39693703uh.html', 
     #'http://www.example.com/us/pantaloni-5-tasche_cod36883777uu.html', 
     #'http://www.example.com/fr/robe_cod34663996xk.html', 
     #'http://www.example.com/fr/trousers_cod36898044mj.html', 
     'http://www.example.com/us/women/onlinestore/suits-and-jackets', 
    ] 

    rules = (
     # Extract links matching 'item.php' and parse them with the spider's method parse_item 
     Rule(LinkExtractor(allow=('http://www.example.com/us/', 'http://www.example.com/fr/',)), follow=True), 
     Rule(LinkExtractor(allow=('.*_cod.*\.html',)), callback='parse_item'), 
    ) 

    def parse_item(self, response): 
     self.logger.info('Hi, this is an item page! %s', response.url) 
     item = SiteItem() 
     item['name'] = response.xpath('//h2[@class="productName"]/text()').extract() 
     item['price'] = response.xpath('//span[@class="priceValue"]/text()')[0].extract() 
     if response.xpath('//span[@class="currency"]/text()')[0].extract() == '$': 
      item['currency'] = 'USD' 
     else: 
      item['currency'] = response.xpath('//span[@class="currency"]/text()')[0].extract() 
     item['category'] = response.xpath('//li[@class="selected leaf"]/a/text()').extract() 
     item['sku'] = response.xpath('//span[@class="MFC"]/text()').extract() 
     if response.xpath('//div[@class="soldOutButton"]/text()').extract() == True or response.xpath('//span[@class="outStock"]/text()').extract() == True: 
      item['avaliability'] = 'No' 
     else: 
      item['avaliability'] = 'Yes' 
     item['time'] = datetime.datetime.now().strftime("%Y.%m.%d %H:%M") 
     item['color'] = response.xpath('//*[contains(@id, "color_")]/a/text()').extract() 
     item['size'] = response.xpath('//*[contains(@id, "sizew_")]/a/text()').extract() 
     if '/us/' in response.url: 
      item['region'] = 'US' 
     elif '/fr/' in response.url: 
      item['region'] = 'FR' 
     item['description'] = response.xpath('//div[@class="descriptionContent"]/text()')[0].extract() 
     return item 

を私は何が欠けていますか?

答えて

0

私はこのウェブサイトがすべての非標準ユーザーエージェントをブロックしているように見せています(403を返すことによって)。

class SiteProducts(CrawlSpider): 
    name = 'site_products' 
    user_agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' 

か、単にプロジェクトsettings.pyでそれを設定します:だからのような一般的なものにuser_agentクラスparemeterを設定してみてください

USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0' 

あなたがウェブの周りに多くのユーザーエージェント文字列を検索することができ、例えばofficial mozzila docummentation

編集:
さらに検査すると、LinkExtractorロジックに問題があることがわかります。 Linkextractorsは、定義されたルールの順番で抽出し、エクストラクターはオーバーフラップします。つまり、最初のlinkextractorと同様にプロダクトページも抽出されます。これは、既に持っているプロダクトリンクエクストラクターが既にクローラーになっているページをクロールし、

製品ページを避けるために、最初のlinkextractorを再加工する必要があります。これは、linkextractorのallowパラメータを、最初のlinkextractorのdenyパラメータにコピーするだけです。

+0

すでにこれをsettings.pyで行っています。すべてのページでCrawled(200)になっています。しかし、私が上で言ったように、私はまだ0スクレープを得ています。しかし、 "start_urls"に製品URLを追加すると、正確な製品をすこぶるように掻き集めることになります。 –

+0

@ LeonidIvanov私はあなたのスパイダーを試しましたが、それは動作しますが、あなたの最初のlinkextractorが製品の抽出をブロックしているようです。 – Granitosaurus

+0

ありがとうございます!それは助けになった!問題は何かを把握するために一日中忘れてしまいました....ところで、それは「否定」ではなく「否定」です。 –

関連する問題