如何使熊猫数据框中的链接可点击?

时间:2019-03-21 20:15:34

标签: python django pandas html-table hyperlink

我目前正在使用beautifulsoup在网站上刮擦一个表,该表包括链接,然后我将该表转换为pandas数据框,并使用pandas的“ to_html”选项将其转换为html,所有这些都在Django中运行

这是我在Python中创建表的方式:

res = []
                for row in table.find_all('tr'):
                    row_data = []
                    for td in row.find_all('td'):
                        td_check = td.find('a')
                        if td_check is not None:
                            link = td.find('a')
                            row_data.append(link)
                        else:
                            not_link = ''.join(td.stripped_strings)
                            if not_link == '':
                                not_link = None
                            row_data.append(not_link)
                    res.append(row_data)

然后我使用以下代码将其转换为HTML:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial")

但是它会像这样在我的网站上输出表格:

Table

我不明白为什么它不可点击?如果我使用浏览器检查表格中的单元格,则HTML为:

<td>
   &lt;a href="https://www.sanger.ac.uk/htgt/wge/crispr/1006029202"&gt;1006029202&lt;/a&gt;
</td>

因此某处的格式出现问题,我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:0)

我弄清楚了,在我的'to_html'中,我必须在最后的括号中添加'escape = False'。

所以我之前的代码:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial")

及之后:

sangerDF = sangerDF.to_html(classes=["table-bordered", "table-striped", "table-hover",], index=False, justify="initial", escape=False)

希望这会有所帮助。