gpt4 book ai didi

python - 类型错误 : writerows() argument must be iterable

转载 作者:行者123 更新时间:2023-12-01 05:10:52 33 4
gpt4 key购买 nike

Python 2.7

我正在尝试获取此页面上的公司名称并将其保存在 csv 文件中。

我的代码的第一部分工作正常,但每个返回的对象(公司名称)之间有空格。

我在编写结果并将其保存在 csv 文件中时也遇到了麻烦,这让我相信这是因为之间的空格使得“数据”不可迭代。

有人可以帮忙修复语法吗?非常感谢!

我的代码(第一部分)

import urllib2
response = urllib2.urlopen('http://app.core-apps.com/weftec2014/exhibitors/list/A')
page = response.read()
page = page[4632:]


def get_next_target(page):
start_link = page.find("<a href='/weftec2014/exhibitors/")
if start_link == -1:
return None, 0
else:
start_place = start_link+73 #to get company names after the first <div>
end_place = page.find("</div>", start_place)
item = page[start_place:end_place]
return item, end_place

def print_all_com(page): #return company names
while True:
item, end_place = get_next_target(page)
if item:
print item
page = page[end_place:]
else:
break


data = print_all_com(page)

第二部分(CSV 编写器)

import csv
with open('weftec_list.csv','w') as f:
writer = csv.writer(f)
writer.writerows(data)

错误消息:

Traceback (most recent call last):
File "/Users/yumiyang/Documents/MCComponenet_crawler.py", line 32, in <module>
writer.writerows(data)
TypeError: writerows() argument must be iterable

最佳答案

我无法测试它,但可能应该是:

def print_all_com(page): #return company names

results = []

while True:
item, end_place = get_next_target(page)
if item:
results.append( [ item.strip() ] )
#print item
page = page[end_place:]
else:
break

return results

关于python - 类型错误 : writerows() argument must be iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253694/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com