我正在使用刮刀从网站上提取街道名称和邮政编码,所有这些工作都非常有效,并且它构建了一个适合我的CSV文件。但是,当我在Excel中打开CSV文件时,文件将有一个空白行,而不是具有街道名称的行,下一列中的邮政编码就像我想要的那样。但接下来我有一个空白行而不是一行,旁边有街道名称和邮政编码。这只是在文件的整个过程中继续进行,该文件在行中输入街道名称和邮政编码,然后在导入PHPMyAdmin数据库时在下一行中显示单词none。我想摆脱空白行。这是我的代码。
from bs4 import BeautifulSoup
import csv
import urllib2
url="http://www.conakat.com/states/ohio/cities/defiance/road_maps/"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
f = csv.writer(open("Defiance Steets1.csv", "w"))
f.writerow(["Street", "Zipcode"]) # Write column headers as the first line
links = soup.find_all('a')
for link in links:
i = link.find_next_sibling('i')
if getattr(i, 'name', None):
a, i = link.string, i.string[1:-1]
f.writerow([a, i])
答案 0 :(得分:2)
这对我有用(我添加了lineterminator ="\n"
):
from BeautifulSoup import BeautifulSoup
import csv
import urllib2
url="http://www.conakat.com/states/ohio/cities/defiance/road_maps/"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
f = csv.writer(open("Defiance Steets1.csv", "w"), lineterminator ="\n")
f.writerow(["Street", "Zipcode"]) # Write column headers as the first line
#print soup.
links = soup.findAll('a')
for link in links:
#i = link.find_next_sibling('i')
i = link.findNextSibling('i')
if getattr(i, 'name', None):
a, i = link.string, i.string[1:-1]
print [a,i]
f.writerow([a, i])