Write to a csv file scrapy

后端 未结 5 785
猫巷女王i
猫巷女王i 2021-01-01 21:16

I want to write to csv file in scrapy

 for rss in rsslinks:
  item = AppleItem()
  item[\'reference_link\'] = response.url
  base_url = get_base_url(response         


        
5条回答
  •  臣服心动
    2021-01-01 21:26

    You need to

    1. Write your header row; then
    2. Write the entry rows for each object.

    You could approach it like:

    fields = ["reference_link", "rss_link"] # define fields to use
    with open(filename,'a+') as f: # handle the source file
        f.write("{}\n".format('\t'.join(str(field) 
                                  for field in fields))) # write header 
        for item in items:
            f.write("{}\n".format('\t'.join(str(item[field]) 
                                  for field in fields))) # write items
    

    Note that "{}\n".format(s) gives the same result as "%s\n" % s.

提交回复
热议问题