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
我错过了什么?
已经在settings.py中做了。我正在为所有页面抓取(200)。但正如我上面所说,我仍然得到0次擦伤。但是,如果我将产品网址添加到“start_urls”,它会刮擦确切的产品就好了。 –
@LeonidIvanov我试过你的蜘蛛,它的工作原理,但它似乎是你的第一个linkextractor阻止产品提取,看到我的编辑更多的细节。 – Granitosaurus
非常感谢!它帮助!几乎要花一整天的时间才能找出问题所在。顺便说一句,“否认”不是“禁止”。 –