【问题标题】:Scrapy - Crawl Multiple Pages Per ItemScrapy - 每个项目抓取多个页面
【发布时间】:2012-06-22 04:10:02
【问题描述】:

我正在尝试为每个项目抓取一些额外的页面以获取一些位置信息。

在返回之前的项目末尾我检查我们是否需要抓取额外的页面来获取信息,本质上这些页面包含一些位置详细信息并且是一个简单的获取请求。

http://site.com.au/MVC/Offer/GetLocationDetails/?locationId=3761&companyId=206

上面的链接要么返回一个包含更多要抓取的页面的选择,要么返回一个带有地址详细信息的 dd/dt。无论哪种方式,我都需要提取此地址信息并将其附加到我的项目['locations']

到目前为止我有(在解析块的末尾)

return self.fetchLocations(locations_selector, company_id, item)

locations_selector 包含 locationIds 列表

那我有

def fetchLocations(self, locations, company_id, item): #response):
    for location in locations:
        if len(location)>1:
            yield Request("http://site.com.au/MVC/Offer/GetLocationDetails/?locationId="+location+"&companyId="+company_id,
            callback=self.parseLocation,
                meta={'company_id': company_id, 'item': item})

最后

def parseLocation(self,response):
    hxs = HtmlXPathSelector(response)
    item = response.meta['item']

    dl = hxs.select("//dl")
    if len(dl)>0:
        address = hxs.select("//dl[1]/dd").extract()
        loc = {'address':remove_entities(replace_escape_chars(replace_tags(address[0], token=' '), replace_by=''))}
        yield loc

    locations_select = hxs.select("//select/option/@value").extract()
    if len(locations_select)>0:
        yield self.fetchLocations(locations_select, response.meta['company_id'], item)

似乎无法正常工作....

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    这是你的代码:

    def parseLocation(self,response):
        hxs = HtmlXPathSelector(response)
        item = response.meta['item']
    
        dl = hxs.select("//dl")
        if len(dl)>0:
            address = hxs.select("//dl[1]/dd").extract()
            loc = {'address':remove_entities(replace_escape_chars(replace_tags(address[0], token=' '), replace_by=''))}
            yield loc
    
        locations_select = hxs.select("//select/option/@value").extract()
        if len(locations_select)>0:
            yield self.fetchLocations(locations_select, response.meta['company_id'], item)
    

    回调必须将请求返回到其他页面或项目。在上面的代码中是看到请求,但不是项目。你有yield loc,但locdict 而不是Item 子类。

    【讨论】:

    • 那我该怎么做呢?
    • 你应该使用yield item 而不是yield loc
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多