爬取Comparison of text editors网页中第一个table,代码如下。可是打开保存文件,发现csv如图所示
与自己设想的第一行是抬头名,记录信息是一行一行的。不一样啊
代码如下,谁能帮我解答下、
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://en.wikipedia.org/wiki/Comparison_of_text_editors")
bsObj = BeautifulSoup(html, "html.parser")
#主对比表格是当前页面上的第一个表格
table = bsObj.findAll("table", {"class": "wikitable"})[0]
rows = table.findAll("tr")
csvFile = open("E:/python/files/editors.csv", 'wt', newline='\n', encoding='utf-8') #newline换行符
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = []
for cell in row.findAll(["td","th"]):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()