首页 > 请问pyspider中一个页面内如果要返回多条数据怎么做?

请问pyspider中一个页面内如果要返回多条数据怎么做?

我试过用yield或者数组返回都不行:
1.yield:只会得到第一条数据
2.数组:会把整个数组当一个字段
我想每一条数据作为一行在result里边,应该怎么做?

from pyspider.libs.base_handler import *
from pyquery import PyQuery


class Handler(BaseHandler):
    crawl_config = {
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('http://www.fortunechina.com/fortune500/c/2015-07/08/content_242859.htm', callback=self.index_page)

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('.f500c2 a').items():
            self.crawl(each.attr.href, callback=self.detail_page)

    @config(priority=2)
    def detail_page(self, response):
        res = {}
        res["industry"] = response.doc('.title').text()[18:]
        for each in response.doc('.contentp tr')[1:]:
            each = PyQuery(each)
            res["name"] = each.find('.f500c3 > a').text()
            res["url"] = each.find('.f500c3 > a').attr.href
            res["income"] = each.find('.f500c4').text().replace(',','')
            yield res//试过这样只有一条数据

把yield 改成return 返回一个数组
然后那些取值在index_page里面获取


参考这里:https://github.com/binux/pyspider/issues/90#issuecomment-71056018

顺带一提,res 最好在循环内定义,否则每次 yield 的都是同一个对象,容易出问题。


如果你要用内置的 resultdb, 用 http://docs.pyspider.org/en/latest/apis/self.send_message/ 为毎一个结果生成一个虚拟 url.

【热门文章】
【热门文章】