【问题标题】:Scrapy parse list of urls, open one by one and parse additional dataScrapy解析url列表,一一打开解析附加数据
【发布时间】: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


    【解决方案1】:

    您正在模拟的 AJAX 请求被 Scrapy“重复 url 过滤器”捕获。

    在生成Request 时将dont_filter 设置为True

    yield Request(url=item['url'], 
                  meta={'item': item},    
                  callback=self.parse_additional_info, 
                  dont_filter=True)
    

    【讨论】:

    • 但我仍然无法使其正常工作。当我在 shell 中尝试时,首先它返回一个 url 列表(四个链接)。然后,替换请求中的每个链接,从每个页面解析添加数据。我试过 Request(url=item['url'][0], etc. ) 但没有任何变化。 =0
    • @deniskrishna 好吧,它肯定会有所作为。它对我有用,这意味着现在我看到为附加信息发布的多个 URL。也有一些例外,但这不是问题的一部分。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2015-05-01
    相关资源
    最近更新 更多