-1

我正在开发一个从网站上提取街道名称和邮政编码的刮板,所有这些都运行良好,它构建了一个 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])
4

1 回答 1

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])
于 2013-10-29T16:10:17.003 回答