List Python 3:My for循环正在替换我的最后一个列表项,而不是附加到它
我有两个问题,但我认为解决一个问题就能解决另一个问题。 我的目标是移动到几个不同的网页并找到包含节点名称的行,我已经设法为此创建了一个for循环,该循环运行良好。唯一的问题是每次my for循环再次运行时,都会从列表中删除最后一个节点名条目,并在其位置添加新条目,从而在列表中只保留一个节点名 与问题相关的完整代码List Python 3:My for循环正在替换我的最后一个列表项,而不是附加到它,list,python-3.x,csv,for-loop,List,Python 3.x,Csv,For Loop,我有两个问题,但我认为解决一个问题就能解决另一个问题。 我的目标是移动到几个不同的网页并找到包含节点名称的行,我已经设法为此创建了一个for循环,该循环运行良好。唯一的问题是每次my for循环再次运行时,都会从列表中删除最后一个节点名条目,并在其位置添加新条目,从而在列表中只保留一个节点名 与问题相关的完整代码 webstringy = "mycompanysite.com/?NodeID=" webpage = "mycompanysite.com/?NetworkID=36" r2
webstringy = "mycompanysite.com/?NodeID="
webpage = "mycompanysite.com/?NetworkID=36"
r2 = s2.get(webpage)
bsobjswap = BeautifulSoup(r2.content)
gotopagenums = [re.findall("\d+", i.get('onclick')) for i in bsobjswap.findAll('tr', attrs={'onclick':True})]
#link = (len(gotopagenums))
print (gotopagenums)
results = open("niki2.csv", 'w', newline='')
wr2 = csv.writer(results, dialect='excel')
for i in gotopagenums:
wr2.writerows([i])
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = [webstringy+nodenojoin]
for weblnky in weblink:
r2 = s2.get(weblnky)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
print (test5)
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in test5:
wr.writerows([i])
wr.writerows('\n')
当我在循环外打印test5时,我得到
['GG Eastbourne']
这是最后一个条目,所以当我尝试写入csv时,它只包含此条目
我需要请知道如何将上面的所有条目放入一个列表中,以便我可以将它们正确打印到.csv
我已经尝试了附加、映射、连接,越来越多的循环我无法理解
来自GAURAV DHAMA的输出
[['GG Alperton']]
[['GG Angel']]
[['GG Ashford']]
[['GG Barking']]
[['GG Bedford']]
[['GG Birmingham']]
[['GG Bolton']]
[['GG Bothwell Street']]
[['GG Bournemouth']]
[['GG Bracknell']]
[['GG Brighton London road']]
[['GG Brighton Madeira']]
[['GG Bristol']]
[['GG Cardiff']]
[['GG Chadwell Heath']]
[['GG Charing Cross']]
[['GG Chelmsford']]
[['GG Colchester']]
[['GG Crawley']]
[['GG Croydon']]
[['GG Dartford']]
[['GG Derby']]
[['GG Ealing']]
[['GG East Croydon']]
[['GG Eastbourne']]
将代码更改为:
mylist = []
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = webstringy+nodenojoin
r2 = s2.get(weblink)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
mylist.append(test5)
print mylist
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in mylist:
wr.writerow(i)
mylist=[]
对于gotopagenums的nodeno:
nodenojoin=“”.join(nodeno)
weblink=webstringy+nodenojoin
r2=s2.get(weblink)
bsobjswap2=BeautifulSoup(r2.content)
nodename=[(bsobjswap2.h1.span)]
测试=[nodename]
test3='\n'.join(str(e)表示测试中的e)
#如果test3.startswith(“[将代码更改为:
mylist = []
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = webstringy+nodenojoin
r2 = s2.get(weblink)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
mylist.append(test5)
print mylist
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in mylist:
wr.writerow(i)
mylist=[]
对于gotopagenums的nodeno:
nodenojoin=“”.join(nodeno)
weblink=webstringy+nodenojoin
r2=s2.get(weblink)
bsobjswap2=BeautifulSoup(r2.content)
nodename=[(bsobjswap2.h1.span)]
测试=[nodename]
test3='\n'.join(str(e)表示测试中的e)
#如果test3.startswith(“[感谢您的回复,但我仍然得到相同的输出,只有列表中的最后一个条目添加了输出现在的外观到我的问题底部,我已经编辑了答案,请再次检查您的结果。通常不是现在,我得到的唯一结果是[['GG Eastbourne']]它现在可以工作了,你在你的代码中创建了很多不必要的列表谢谢你的回复,但是我仍然得到了相同的输出,只有最后一个条目在列表中。在我的问题底部添加了输出的外观。我已经编辑了答案,请再次检查你的结果。现在我得到的唯一结果是[['GG Eastbourne']]现在可以了,您在代码中创建了很多不必要的列表
mylist = []
for nodeno in gotopagenums:
nodenojoin = "".join(nodeno)
weblink = webstringy+nodenojoin
r2 = s2.get(weblink)
bsobjswap2 = BeautifulSoup(r2.content)
nodename = [(bsobjswap2.h1.span)]
test = [nodename]
test3 = '\n'.join(str(e) for e in test)
#if test3.startswith("[<span"):
# if test3.endswith("</span>]"):
test4 = (test3[72:])
test5 = (test4[:-9])
test5 = [test5]
mylist.append(test5)
print mylist
resultfile = open("niki.csv", 'w')
wr = csv.writer(resultfile, delimiter=',', dialect='excel')
for i in mylist:
wr.writerow(i)