我使用Beautiful Soup来从几个页面中的URL中找到特定表 url.csv
代码:
def parse_csv(content, delimiter = ';'):
csv_data = []
for line in content.split('\n'):
csv_data.append( [x.strip() for x in line.split( delimiter )] ) # strips spaces also
return csv_data
list_url=parse_csv(open('url.csv','rU').read())
f = csv.writer(open("raw.csv", "w",encoding='utf8',newline=''))
# Write column headers as the first line
for i in range (0,len(list_url)):
url=str(list_url[i][0]) ## read URL from an array coming from an Url-CSV
page=urllib.request.urlopen(url)
soup = BeautifulSoup(page.read(),"html.parser")
restricted_webpage= soup.find( "div", {"id":"ingredients"} )
readable_restricted=str(restricted_webpage)
soup2=BeautifulSoup(readable_restricted,"html.parser")
links = soup2.find_all('td')
print(len(links))
for link in links:
i = link.find_next_sibling('td')
if getattr(i, 'name', None):
a, i = link.string, i.string
f.writerow([a, i])
我的CSV看起来像:
"
Cendres brutes (%)
","
7.4
" "
Cellulose brute (%)
","
1.6
" "
Fibres alimentaires (%)
","
6.6
" "
Matière grasse (%)
","
16.0
而我希望它看起来像:
Cendres brutes(%);7.4
Cellulose brute (%);1.6
Fibres Alimentaires(%);6.6
Mati̬re grasse (%);16.0
我需要它看起来像有两个原因:1.当我在excel中打开此类CSV时,它看起来很棒。2.我可以使用CSV解析器(在第一行定义一个parse_csv
)并处理从CSV生成的数组,就像它是excel中的单元格一样。单元格[x] [y]。很少。
我该如何实现?是说拥有我想要的CSV类型?
csv_writer = csv.writer(outfile, delimiter=';')
用分号转换昏迷。可以由excel-EU读取。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句