bla*_*mba 12 python csv scrapy
我想在scrapy中写入csv文件
for rss in rsslinks:
item = AppleItem()
item['reference_link'] = response.url
base_url = get_base_url(response)
item['rss_link'] = urljoin_rfc(base_url,rss)
#item['rss_link'] = rss
items.append(item)
#items.append("\n")
f = open(filename,'a+') #filename is apple.com.csv
for item in items:
f.write("%s\n" % item)
Run Code Online (Sandbox Code Playgroud)
我的输出是这样的:
{'reference_link': 'http://www.apple.com/'
'rss_link': 'http://www.apple.com/rss '
{'reference_link': 'http://www.apple.com/rss/'
'rss_link': 'http://ax.itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/newreleases/limit=10/rss.xml'}
{'reference_link': 'http://www.apple.com/rss/'
'rss_link': 'http://ax.itunes.apple.com/WebObjects/MZStore.woa/wpa/MRSS/newreleases/limit=25/rss.xml'}
Run Code Online (Sandbox Code Playgroud)
我想要的是这种格式:
reference_link rss_link
http://www.apple.com/ http://www.apple.com/rss/
Run Code Online (Sandbox Code Playgroud)
Guy*_*ely 70
只需抓取-o csv
,像:
scrapy crawl <spider name> -o file.csv -t csv
Run Code Online (Sandbox Code Playgroud)
jwa*_*man 16
这就是使用 Python3 对我有用的方法:
scrapy runspider spidername.py -o file.csv -t csv
Run Code Online (Sandbox Code Playgroud)
你需要
你可以这样处理:
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
Run Code Online (Sandbox Code Playgroud)
请注意"{}\n".format(s)
给出的结果与 相同"%s\n" % s
。
归档时间: |
|
查看次数: |
27333 次 |
最近记录: |