默认的顺序是字母表,我已经阅读了一些文章来使用OrderedDict输出项目的定制顺序。
我写了一只蜘蛛跟随网页。
How to get order of fields in Scrapy item
我的items.py。
import scrapy
from collections import OrderedDict
class OrderedItem(scrapy.Item):
def __init__(self, *args, **kwargs):
self._values = OrderedDict()
if args or kwargs:
for k, v in six.iteritems(dict(*args, **kwargs)):
self[k] = v
class StockinfoItem(OrderedItem):
name = scrapy.Field()
phone = scrapy.Field()
address = scrapy.Field()简单的蜘蛛文件。
import scrapy
from info.items import InfoItem
class InfoSpider(scrapy.Spider):
name = 'Info'
allowed_domains = ['quotes.money.163.com']
start_urls = [ "http://quotes.money.163.com/f10/gszl_600023.html"]
def parse(self, response):
item = InfoItem()
item["name"] = response.xpath('/html/body/div[2]/div[4]/table/tr[2]/td[2]/text()').extract()
item["phone"] = response.xpath('/html/body/div[2]/div[4]/table/tr[7]/td[4]/text()').extract()
item["address"] = response.xpath('/html/body/div[2]/div[4]/table/tr[2]/td[4]/text()').extract()
item.items()
yield item什么时候运行蜘蛛的信息。
2019-04-25 13:45:01 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.money.163.com/f10/gszl_600023.html>
{'address': ['浙江省杭州市天目山路152号浙能大厦'],'name': ['浙能电力'],'phone': ['0571-87210223']}为什么我不能得到如下所需的订单?
{'name': ['浙能电力'],'phone': ['0571-87210223'],'address': ['浙江省杭州市天目山路152号浙能大厦']}感谢Gallaecio的建议,在settings.py中添加以下内容。
FEED_EXPORT_FIELDS=['name','phone','address']执行蜘蛛并输出到csv文件。
scrapy crawl info -o info.csv字段顺序是我定制的顺序。
cat info.csv
name,phone,address
浙能电力,0571-87210223,浙江省杭州市天目山路152号浙能大查看scrapy的调试信息:
2019-04-26 00:16:38 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.money.163.com/f10/gszl_600023.html>
{'address': ['浙江省杭州市天目山路152号浙能大厦'],
'name': ['浙能电力'],
'phone': ['0571-87210223']}如何按自定义顺序制作调试信息?如何获得以下调试输出?
2019-04-26 00:16:38 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.money.163.com/f10/gszl_600023.html>
{'name': ['浙能电力'],
'phone': ['0571-87210223'],
'address': ['浙江省杭州市天目山路152号浙能大厦'],}发布于 2019-04-30 16:01:36
问题在于__repr__函数的Item。其代码最初是:
def __repr__(self):
return pformat(dict(self))因此,即使您将项转换为OrderedDict并期望字段按相同的顺序保存,此函数也会对其应用dict()并打破顺序。
所以,我建议你以你喜欢的方式让它超载,例如:
import json
class OrderedItem(scrapy.Item):
def __init__(self, *args, **kwargs):
self._values = OrderedDict()
if args or kwargs:
for k, v in six.iteritems(dict(*args, **kwargs)):
self[k] = v
def __repr__(self):
return json.dumps(OrderedDict(self), ensure_ascii = False) # it should return some string现在您可以得到这个输出:
2019-04-30 18:56:20 [scrapy.core.scraper] DEBUG: Scraped from <200 http://quotes.money.163.com/f10/gszl_600023.html>
{"name": ["\u6d59\u80fd\u7535\u529b"], "phone": ["0571-87210223"], "address": ["\u6d59\u6c5f\u7701\u676d\u5dde\u5e02\u5929\u76ee\u5c71\u8def152\u53f7\u6d59\u80fd\u5927\u53a6"]}发布于 2019-04-28 09:01:20
可以定义项的自定义字符串表示形式。
class InfoItem:
def __repr__(self):
return 'name: {}, phone: {}, address: {}'.format(self['name'], self.['phone'], self.['address'])发布于 2019-04-28 09:18:47
在将item.items()替换为self.log(item.items())的蜘蛛中,log应该是元组列表,以便在蜘蛛中分配它们。
另一种方法是将您在文章中提到的答案与this answer结合起来。
https://stackoverflow.com/questions/55851125
复制相似问题