【发布时间】:2020-03-05 19:01:00
【问题描述】:
这是我正在抓取的网页的 HTML 数据,您可以看到它有多个标签。 (https://paste.pythondiscord.com/resaxivedo.py)
This is my code:
with open("tabledata.html", "r") as f:
contents = f.read()
outfile = open("table_data.csv", "w", newline='')
writer = csv.writer(outfile)
tree = BeautifulSoup(contents, "lxml")
dates = tree.findAll(class_="date")
list_of_dates = [date.text for date in dates]
table_tag = tree.select("table")[0]
tab_data = [[item.text for item in row_data.select("th,td")]
for row_data in table_tag.select("tr")]
writer.writerow(list_of_dates[0])
for data in tab_data:
print(' '.join(data))
writer.writerow(data)
如您所见,我使用 [0] 选择表格和日期。 如何创建一个循环,以便打印 HTML 页面中所有表格的数据?
【问题讨论】:
标签: python loops web-scraping html-table