我正在尝试解析一个网站,一个电子商店。我解析一个包含ajax产品的页面,获取这些产品的网址,然后根据这些分区网址解析每个产品的其他信息。
我的脚本获取页面上前4个项目的列表,它们的URL,发出请求,解析添加信息,但之后不返回循环,因此蜘蛛关闭。
有人可以帮我解决这个问题吗?我对这种东西很陌生,在完全卡住的时候问这里。
这是我的代码:
from scrapy import Spider
from scrapy.selector import Selector
from scrapy.http.request import Request
from scrapy_sokos.items import SokosItem
class SokosSpider(Spider):
name = "sokos"
allowed_domains = ["sokos.fi"]
base_url = "http://www.sokos.fi/fi/SearchDisplay?searchTermScope=&searchType=&filterTerm=&orderBy=8&maxPrice=&showResultsPage=true&beginIndex=%s&langId=-11&sType=SimpleSearch&metaData=&pageSize=4&manufacturer=&resultCatEntryType=&catalogId=10051&pageView=image&searchTerm=&minPrice=&urlLangId=-11&categoryId=295401&storeId=10151"
start_urls = [
"http://www.sokos.fi/fi/SearchDisplay?searchTermScope=&searchType=&filterTerm=&orderBy=8&maxPrice=&showResultsPage=true&beginIndex=0&langId=-11&sType=SimpleSearch&metaData=&pageSize=4&manufacturer=&resultCatEntryType=&catalogId=10051&pageView=image&searchTerm=&minPrice=&urlLangId=-11&categoryId=295401&storeId=10151",
]
for i in range(0, 8, 4):
start_urls.append((base_url) % str(i))
def parse(self, response):
products = Selector(response).xpath('//div[@class="product-listing product-grid"]/article[@class="product product-thumbnail"]')
for product in products:
item = SokosItem()
item['url'] = product.xpath('//div[@class="content"]/a[@class="image"]/@href').extract()[0]
yield Request(url = item['url'], meta = {'item': item}, callback=self.parse_additional_info)
def parse_additional_info(self, response):
item = response.meta['item']
item['name'] = Selector(response).xpath('//h1[@class="productTitle"]/text()').extract()[0].strip()
item['description'] = Selector(response).xpath('//div[@id="kuvaus"]/p/text()').extract()[0]
euro = Selector(response).xpath('//strong[@class="special-price"]/span[@class="euros"]/text()').extract()[0]
cent = Selector(response).xpath('//strong[@class="special-price"]/span[@class="cents"]/text()').extract()[0]
item['price'] = '.'.join(euro + cent)
item['number'] = Selector(response).xpath('//@data-productid').extract()[0]
yield item
答案 0 :(得分:0)
您正在模拟的AJAX请求被Scrapy"重复的URL过滤器"捕获。
在产生dont_filter
时将True
设为Request
:
yield Request(url=item['url'],
meta={'item': item},
callback=self.parse_additional_info,
dont_filter=True)