Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用用户';使用scrapy将其他元素添加到csv中_Python_Date_Dictionary_Scrapy_Export To Csv - Fatal编程技术网

Python 使用用户';使用scrapy将其他元素添加到csv中

Python 使用用户';使用scrapy将其他元素添加到csv中,python,date,dictionary,scrapy,export-to-csv,Python,Date,Dictionary,Scrapy,Export To Csv,我有一个使用scrapy的运行脚本,它从表中获取数据。但它以该格式保存,因为原始数据按行参数顺序排列: name firstitem seconditem ... lastitem 如果没有像“name”这样的“name”,如何以行格式保存此dict 21:00 2019/02/22, firstitem, seconditem,...,lastitem 我已经有了包含当前时间的列表,所以我需要将此dict重写为一个列表,以便将其解析为CSV EDIT我用current_time参数替换了

我有一个使用scrapy的运行脚本,它从表中获取数据。但它以该格式保存,因为原始数据按行参数顺序排列:

name 
firstitem
seconditem
...
lastitem
如果没有像“name”这样的“name”,如何以行格式保存此dict

21:00 2019/02/22, firstitem, seconditem,...,lastitem
我已经有了包含当前时间的列表,所以我需要将此dict重写为一个列表,以便将其解析为CSV

EDIT我用current_time参数替换了dictionary的键,但输出格式仍然存在问题

将刮屑作为sp导入
从时间导入gmtime,strftime
当前时间=strftime(“%Y-%m-%d%H:%m:%S”,gmtime())
类表Spider(sp.Spider):
name='spider'
start_url=['example.com']#无法公开真实的url
def解析(自我,响应):
CLASS_选择器='.col-xs-3'
对于ex in response.css(类选择器):
名称\选择器='a:not(.dep)::text'
屈服{
当前时间:ex.css(名称选择器).extract\u first(),
}
从scrapy.crawler导入crawler进程
c=爬网进程({
“用户代理”:“Chrome/72.0.3626.119”,
“提要格式”:“csv”,
“FEED_URI”:“booking.csv”,
})
c、 爬行(表皮德尔)
c、 开始()
编辑 具有替换值的目标Html代码(我需要所有“项”的值):


#
桌子
描述
1.

日期 副词。 2.
日期 副词。 3.
日期 副词。
项目/项目加载器机制满足您的需要。比如:

为数据行定义项目:

class DataRowItem(scrapy.Item):
     current_time = scrapy.Field()
     firstitem = scrapy.Field()
     ...
然后声明匹配的ItemLoader:

class DataRowItemLoader(scrapy.ItemLoader):
    default_item_class = DataRowItem
    default_output_processor = TakeFirst()
在解析函数中:

def parse(self, response):
    loader = DataRowItemLoader(DataRowItem(), response=response)
    ... Extract the data here, using loader methods ...
    loader.add_css('current_time', ...)
    loader.add_css('firstitem', ...)
    ...
    yield loader.load_item()  # One item = one line

然后使用以下方法序列化CSV中的项目:

项目/项目加载器机制符合您的目的。比如:

为数据行定义项目:

class DataRowItem(scrapy.Item):
     current_time = scrapy.Field()
     firstitem = scrapy.Field()
     ...
然后声明匹配的ItemLoader:

class DataRowItemLoader(scrapy.ItemLoader):
    default_item_class = DataRowItem
    default_output_processor = TakeFirst()
在解析函数中:

def parse(self, response):
    loader = DataRowItemLoader(DataRowItem(), response=response)
    ... Extract the data here, using loader methods ...
    loader.add_css('current_time', ...)
    loader.add_css('firstitem', ...)
    ...
    yield loader.load_item()  # One item = one line

然后使用以下方法序列化CSV中的项目:

可能的重复您可以添加一些替换或删除实际值的示例HTML吗?您使用的
col-xs-3
表明它使用的不是
,而是引导(或类似)网格。当然,但我认为这不会帮助您解决我的问题,因为提取的值格式错误。我编辑了这个问题并添加了下面的html。你们能添加一些替换或删除真实值的html示例吗?您使用的
col-xs-3
表明它使用的不是
,而是引导(或类似)网格。当然,但我认为这不会帮助您解决我的问题,因为提取的值格式错误。我编辑了这个问题并添加了下面的html。