【问题标题】:How to make links in pandas dataframe clickable?如何使熊猫数据框中的链接可点击?
【发布时间】:2019-08-12 18:58:49
【问题描述】:

我目前正在使用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")

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

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

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

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

谢谢!

【问题讨论】:

    标签: python django pandas html-table hyperlink


    【解决方案1】:

    我想通了,我必须在我的 '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)
    

    希望这会有所帮助。

    【讨论】:

    • 这完全帮助了我。我正在使用格式化程序将 TD 值转换为链接;在我添加 escape=False 标志之前它不会呈现。
    猜你喜欢
    • 2020-07-03
    • 2016-05-20
    • 2021-10-19
    • 2021-10-25
    • 2016-03-09
    • 2018-03-04
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多