【发布时间】:2021-10-15 01:19:52
【问题描述】:
我想将带有 div 元素的表格提取为 ind-mp_info 到 csv 文件。展开 COVID-19 Statewise Status 选项卡时可以找到它。
网址为-https://www.mygov.in/covid-19/
代码-
# importing the libraries
from bs4 import BeautifulSoup
from urllib.request import urlopen
import csv
import pandas as pd
html = urlopen("https://www.mygov.in/covid-19/")
soup = BeautifulSoup(html, "html.parser")
table = soup.find("table", {"class":"ind-mp_info"})
rows = table.findAll("tr")
with open("editors.csv", "wt+", newline="") as f:
writer = csv.writer(f)
for row in rows:
csv_row = []
for cell in row.findAll(["td", "th"]):
csv_row.append(cell.get_text())
writer.writerow(csv_row)
【问题讨论】:
-
...我在哪里可以找到您的代码?
-
请立即查看
-
汤好喝吗?刮痧部分好吗?问题出在哪里?
-
实际上
table没有得到任何数据。它抛出为NoneType。 -
你的问题是网站上的表格是用javascript添加的,beautifulsoup没有执行javascript。您可以使用 selenium,它使用 python 来控制网络浏览器,但@chitown88 的答案是一个更好的方法。
标签: python web-scraping beautifulsoup export-to-csv data-analysis