【问题标题】:How do parse an HTML correlation matrix with Python and Pandas?如何用 Python 和 Pandas 解析 HTML 相关矩阵?
【发布时间】:2018-02-03 23:26:23
【问题描述】:

我已经访问了表格并将信息带入 python,但我无法遍历列名和行名以使用正确的相关值填充表格。如何遍历第一行以提取第 col 标头值?

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

req = Request('https://www.mrci.com/special/corr030.php',headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req, context = ctx).read()

soup = BeautifulSoup(webpage, 'lxml') # Parse the HTML as a string
table = soup.find_all('table')[2] # Grab the first table
for row in table.find_all('tr')[1:]:
    print(row)

有人对我如何将整个表格拉入熊猫数据框有任何见解吗?

【问题讨论】:

标签: python pandas beautifulsoup


【解决方案1】:

你没有显示出你理想的结果,所以我猜,也许代码会对你有所帮助。

for row in table.find_all('tr')[1:]:
    for i in row.descendants:
        print(i)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多