我试过用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//试过这样只有一条数据
如果你要用内置的 resultdb, 用 http://docs.pyspider.org/en/latest/apis/self.send_message/ 为毎一个结果生成一个虚拟 url.