Py学习  »  Python

如何在python中使用beautifulsoup创建链接?

s123 • 4 年前 • 586 次点击  

我正在尝试构建一个HTML页面,该页面有一个包含信息行的表(测试用例、失败、警告、测试总数) 我希望测试用例列中的每一行都是指向另一个页面的链接。 如下图所示, 我的目标是让测试1成为一个链接 . enter image description here 下面是我为构建您在图像中看到的内容而编写的代码。 谢谢。

import bs4
f = open("practice.html", 'w')
html = """<html>
                      <body>
                          <table class="details" border="1" cellpadding="5" cellspacing="2" style="width:95%">
                          </table>
                      </body>
                  </html>"""
soup = bs4.BeautifulSoup(html, "lxml")
table = soup.find('table')
tr = bs4.Tag(table, name='tr')
HTMLColumns = ["Test Cases", "Failed", "Warning", "Total number of tests"]
for title in HTMLColumns:  # Add titles to each column
        td = bs4.Tag(tr, name='td')
        td.insert(0, title)
        td.attrs['style'] = 'background-color: #D6FCE9; font-weight: bold;'
        tr.append(td)
table.append(tr)
results = ["Test 1", str(5), str(3), str(6)]
tr = bs4.Tag(table, name='tr')
for index, r in enumerate(results):  # loop through whole list of issue tuples, and create rows
        td = bs4.Tag(tr, name='td')
        td.attrs['style'] = 'background-color: #ffffff; font-weight: bold;'
        td.string = str(r)
        tr.append(td)
table.append(tr)

f.write(soup.prettify())
f.close()

下面是我从BeautifulSoup文档中获取的用于创建链接的代码:

from bs4 import BeautifulSoup

soup = BeautifulSoup("<b></b>", "lxml")
original_tag = soup.b

new_tag = soup.new_tag("a", href="http://www.example.com")
original_tag.append(new_tag)
original_tag
# <b><a href="http://www.example.com"></a></b>

new_tag.string = "Link text."
original_tag
# <b><a href="http://www.example.com">Link text.</a></b>
f = open("practice.html", 'w')
f.write(soup.prettify())
f.close()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38718
 
586 次点击  
文章 [ 1 ]  |  最新文章 4 年前
kamal yassin
Reply   •   1 楼
kamal yassin    5 年前
# This is updated code 
# You just need to add: a = bs4.Tag(td, name='a') to you'r code 
# Then you need to fill it:

    #     if index == 0: 
    #         a.attrs[''] = 'a href="http://www.yahoo.com"'
    #     a.string = r  
    #     td.append(a)



import bs4
f = open("practice.html", 'w')
html = """<html>
                      <body>
                          <table class="details" border="1" cellpadding="5" cellspacing="2" style="width:95%">
                          </table>
                      </body>
                  </html>"""
soup = bs4.BeautifulSoup(html, "lxml")
table = soup.find('table')
tr = bs4.Tag(table, name='tr')
HTMLColumns = ["Test Cases", "Failed", "Warning", "Total number of tests"]
for title in HTMLColumns:  # Add titles to each column
    td = bs4.Tag(tr, name='td')
    td.insert(0, title)
    td.attrs['style'] = 'background-color: #D6FCE9; font-weight: bold;'
    tr.append(td)
table.append(tr)
results = [str(k), str(v), str(0), str(v)]
tr = bs4.Tag(table, name='tr')
for index, r in enumerate(results):  # loop through whole list of issue tuples, and create rows
    td = bs4.Tag(tr, name='td')
    td.attrs['style'] = 'background-color: #ffffff; font-weight: bold;'
    a = bs4.Tag(td, name='a') 
    if index == 0:
        a.attrs[''] = 'a href="http://www.yahoo.com"'
    a.string = r
    td.append(a)
    tr.append(td)
table.append(tr)  # append the row to the table 
f.write(soup.prettify())
f.close()