3

我正在使用漂亮表在 python 中创建一个表,并试图让一个作为 url 的列具有可点击的链接:

例如:

filepath = "pretty_table.html"

from prettytable import PrettyTable
x = PrettyTable()
x.format = True

x = PrettyTable(["City name", "Area", "Url"])

x.add_row(["Adelaide",1295, "<a href='https://en.wikipedia.org/wiki/Adelaide'>https://en.wikipedia.org/wiki/Adelaide</a>" ])
x.add_row(["Brisbane",5905, "<a href='https://en.wikipedia.org/wiki/Brisbane'>https://en.wikipedia.org/wiki/Brisbane</a>"])

result = []
result.append(x.get_html_string())

with open(filepath, 'w') as f:
    for line in result:
        f.write("{}\n".format(line))

但是,对于上述情况,该表不会为 wiki 页面生成可点击的链接。有人可以帮忙吗。

4

1 回答 1

1

这是答案

    import html
    url = 'https://www.baidu.com' # link to point to
    pt.add_row(['2018-10-10','<a href=\"' + url + '\">' + url + '</a>'])
    text = pt.get_html_string(format=True)
    text = html.unescape(text)   # crucial step

更多信息可以在这里找到

于 2020-12-20T17:50:11.540 回答