【发布时间】:2015-08-17 14:08:02
【问题描述】:
我正在尝试解析一个网站,一个电子商店。我解析一个带有 ajax 加载的产品的页面,获取这些产品的 url,然后在这些 parced url 之后解析每个产品的附加信息。
我的脚本获取页面上前 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
【问题讨论】:
标签: python parsing web-scraping scrapy