python - Scrapy - 抓取每个项目的多个页面

标签 python scrapy

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

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

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

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

到目前为止我已经(在解析 block 的末尾)

return self.fetchLocations(locations_selector, company_id, item)

locations_selector 包含 locationId 列表

那我有

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)

似乎无法让这个工作......

最佳答案

这是您的代码:

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 子类。

关于python - Scrapy - 抓取每个项目的多个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11150053/

相关文章:

python - Scrapy/Python 中的简单信号处理给出错误

套接字错误 Errno 10060 的 Python try/except 语句

Python:计算从一个目录到另一个目录的相对路径

python - 部署 Django 项目时 Vercel CLI Python 版本问题

javascript - 使用Selenium + Scrapy

java - java里面的Scrapy?

python - 如何读取div id

caching - 如何禁用scrapy中的缓存?

python - 为什么 eval 函数中的 lambda 无法关闭用户定义的 'locals' 字典中的变量?

python - sklearn.tree.DecisionTreeClassifier : Get all samples that fell into leaf node