文件显示为"已修改"但仍然是0kb并且是空的。
使用以下代码:
import csv
from craigslist import CraigslistForSale
cl_f = CraigslistForSale (site = 'boston', area = 'gbs', category = 'sys',
filters = {'min_price':5, 'max_price':300, 'has_image':True,
'query':"by owner",'query':"imac"})
for result in cl_f.get_results(sort_by='newest',limit=5):
print result
with open('CL-iMac.csv', 'wb') as f:
writer = csv.writer(f, delimiter=',')
通读其他"类似"问题,但仍然不清楚如何解决这个问题。
添加
后的打印结果for result in cl_f.get_results(sort_by='newest',limit=5):
writer.writerow(result)
:
{'name': u'iMac Mid 2009', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/imac-mid-2009/6504630631.html', 'has_map': True, 'price': u'$190', 'geotag': None, 'where': u'Bedford', 'id': u'6504630631', 'datetime': u'2018-03-12 21:03'}
{'name': u'Apple - iMac Model A1311', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-imac-model-a1311/6525426093.html', 'has_map': True, 'price': u'$180', 'geotag': None, 'where': u'BOSTON', 'id': u'6525426093', 'datetime': u'2018-03-12 19:57'}
{'name': u'Apple iMac 5.1 Model A1208 17" All-in-one', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-imac-51-model-aall-in/6525156610.html', 'has_map': True, 'price': u'$80', 'geotag': None, 'where': u'HYDE PARK', 'id': u'6525156610', 'datetime': u'2018-03-12 13:43'}
{'name': u'imac computer 20 inch', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/imac-computer-20-inch/6488426996.html', 'has_map': True, 'price': u'$210', 'geotag': None, 'where': u'Medford', 'id': u'6488426996', 'datetime': u'2018-03-08 11:17'}
{'name': u'Apple Magic Mouse & Keyboard Set', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-magic-mouse-keyboard-set/6521325579.html', 'has_map': True, 'price': u'$80', 'geotag': None, 'where': u'Malden', 'id': u'6521325579', 'datetime': u'2018-03-06 21:12'}
Traceback (most recent call last):
File "/Documents/Test.py", line 13, in <module>
writer.writerow(result)
Error: sequence expected
答案 0 :(得分:0)
使用生成器将数据作为字典返回。
这将一次返回每个字典1。
您希望它位于列表中,以便您可以迭代它。
您必须将每个字典附加到列表中。
#Create a list to hold the results.
data_parsed = []
#append each result to the list
for result in cl_f.get_results(sort_by='newest',limit=5):
data_parsed.append(result)
然后您可以将数据写入csv,如下所示
with open('CL-iMac.csv', 'w') as f:
#Create writer
writer = csv.writer(f)
#Write header row from 1st item keys
# name,has_image,url,has_map,price,geotag,where,id,datetime
header = data_parsed[0].keys()
writer.writerow(header)
#Write each row from the values
# iMac Mid 2009,True,https://boston.craigslist.org/gbs/sys/d/imac-mid-2009/6504630631.html,True,$190,,Bedford,6504630631,2018-03-12 21:03
for result in data_parsed:
writer.writerow(result.values())
如果我认为
data_parsed = [{'name': u'iMac Mid 2009', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/imac-mid-2009/6504630631.html', 'has_map': True, 'price': u'$190', 'geotag': None, 'where': u'Bedford', 'id': u'6504630631', 'datetime': u'2018-03-12 21:03'},
{'name': u'Apple - iMac Model A1311', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-imac-model-a1311/6525426093.html', 'has_map': True, 'price': u'$180', 'geotag': None, 'where': u'BOSTON', 'id': u'6525426093', 'datetime': u'2018-03-12 19:57'},
{'name': u'Apple iMac 5.1 Model A1208 17" All-in-one', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-imac-51-model-aall-in/6525156610.html', 'has_map': True, 'price': u'$80', 'geotag': None, 'where': u'HYDE PARK', 'id': u'6525156610', 'datetime': u'2018-03-12 13:43'},
{'name': u'imac computer 20 inch', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/imac-computer-20-inch/6488426996.html', 'has_map': True, 'price': u'$210', 'geotag': None, 'where': u'Medford', 'id': u'6488426996', 'datetime': u'2018-03-08 11:17'},
{'name': u'Apple Magic Mouse & Keyboard Set', 'has_image': True, 'url': u'https://boston.craigslist.org/gbs/sys/d/apple-magic-mouse-keyboard-set/6521325579.html', 'has_map': True, 'price': u'$80', 'geotag': None, 'where': u'Malden', 'id': u'6521325579', 'datetime': u'2018-03-06 21:12'}]
然后上面的代码创建了以下csv数据。
name,has_image,url,has_map,price,geotag,where,id,datetime
iMac Mid 2009,True,https://boston.craigslist.org/gbs/sys/d/imac-mid-2009/6504630631.html,True,$190,,Bedford,6504630631,2018-03-12 21:03
Apple - iMac Model A1311,True,https://boston.craigslist.org/gbs/sys/d/apple-imac-model-a1311/6525426093.html,True,$180,,BOSTON,6525426093,2018-03-12 19:57
"Apple iMac 5.1 Model A1208 17"" All-in-one",True,https://boston.craigslist.org/gbs/sys/d/apple-imac-51-model-aall-in/6525156610.html,True,$80,,HYDE PARK,6525156610,2018-03-12 13:43
imac computer 20 inch,True,https://boston.craigslist.org/gbs/sys/d/imac-computer-20-inch/6488426996.html,True,$210,,Medford,6488426996,2018-03-08 11:17
Apple Magic Mouse & Keyboard Set,True,https://boston.craigslist.org/gbs/sys/d/apple-magic-mouse-keyboard-set/6521325579.html,True,$80,,Malden,6521325579,2018-03-06 21:12