2016-11-26 238 views
0

问题是,如果我直接将产品网址添加到“start_urls”,一切正常。但是,当爬网过程中出现的产品页面(所有抓取网页返回“200”),它不凑.... 我通过运行蜘蛛:Scrapy抓取但不会刮

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)。因此,尝试user_agent类paremeter设置为一样共同的东西:

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 = '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中做了。我正在为所有页面抓取(200)。但正如我上面所说,我仍然得到0次擦伤。但是,如果我将产品网址添加到“start_urls”,它会刮擦确切的产品就好了。 –

+0

@LeonidIvanov我试过你的蜘蛛,它的工作原理,但它似乎是你的第一个linkextractor阻止产品提取,看到我的编辑更多的细节。 – Granitosaurus

+0

非常感谢!它帮助!几乎要花一整天的时间才能找出问题所在。顺便说一句,“否认”不是“禁止”。 –