尝试将新行附加到 table body 和 BeautifulSoup 中的第一行

Trying to append a new row to the first row in a the table body with BeautifulSoup

无法将新行附加到 table body () 中的第一行(header 行)。

我的代码:

from bs4 import BeautifulSoup  

soup = BeautifulSoup(open('page_content.xml'), 'html.parser')

# append a row to the first row in the table body
row = soup.find('tbody').find('tr')
row.append(soup.new_tag('tr', text='New Cell'))

print(row)

输出:

<tr>
<th>Version</th>
<th>Jira</th>
<th colspan="1">Date/Time</th>
<tr text="New Cell"></tr></tr>

输出应该是什么:

<tr>
<th>Version</th>
<th>Jira</th>
<th colspan="1">Date/Time</th>
</tr>
<tr text="New Cell"></tr>

完整的 xml 文件是:

<h1>Rental Agreement/Editor</h1>
<table class="wrapped">
<colgroup>
<col/>
<col/>
<col/>
</colgroup>
<tbody>
<tr>
<th>Version</th>
<th>Jira</th>
<th colspan="1">Date/Time</th>
<tr text="New Cell"></tr></tr>
<tr>
<td>1.0.1-0</td>
<td>ABC-1234</td>
<td colspan="1">
<br/>
</td>
</tr>
</tbody>
</table>
<p class="auto-cursor-target">
<br/>
</p>

您可以使用 .insert_after:

from bs4 import BeautifulSoup

html_doc = """
<table>
<tr>
<th>Version</th>
<th>Jira</th>
<th colspan="1">Date/Time</th>
</tr>

<tr>
<td> something else </td>
</tr>
</table>
"""

soup = BeautifulSoup(html_doc, "html.parser")

row = soup.select_one("tr:has(th)")
row.insert_after(soup.new_tag("tr", text="New Cell"))

print(soup.prettify())

打印:

<table>
 <tr>
  <th>
   Version
  </th>
  <th>
   Jira
  </th>
  <th colspan="1">
   Date/Time
  </th>
 </tr>
 <tr text="New Cell">
 </tr>
 <tr>
  <td>
   something else
  </td>
 </tr>
</table>

编辑:如果你想插入任意HTML代码,你可以尝试:

what_to_insert = BeautifulSoup(
    '<tr param="xxx">This is new <b>text</b></tr>', "html.parser"
)

row.insert_after(what_to_insert)