How to insert a new row to html file by using python beautifulesoup

Question:

I am using python 2.7, beautifulsoup. I have a local html file and I want to insert information in a table by appending new rows to it.

After searching from web, I am able to pick the correct table which is no id.

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('./result.html'), 'html.parser')

table = soup.find(text="Beta").find_parent("table")
for row in table.find_all("tr")[1:]:
    print([cell.get_text(strip=True) for cell in row.find_all("td")])

Here is what I want to do. Before:

<tr><td colspan="3"><hr></td></tr>
<tr><td>Beta</td><td>0.0883</td><td>-</td></tr>
<tr><td>Alpha</td><td>0.1115</td><td>-</td></tr>
</tbody>
</table>

After:

<tr><td colspan="3"><hr></td></tr>
<tr><td>Beta</td><td>0.0883</td><td>-</td></tr>
<tr><td>Alpha</td><td>0.1115</td><td>-</td></tr>
<tr><td colspan="3"><hr></td></tr>
<tr><td>Total Trade</td><td>2574</td><td>-</td></tr>
</tbody>
</table>

Thank you very much.

Asked By: sflee

||

Answers:

new_tag = soup.new_tag("Your Tag") #add even more tags as needed
new_tag.append("Your Text")
# insert the new tag after the last tag using insert_after
the_last_tag_i_want_to_insert_after.insert_after(new_tag)
Answered By: TheLazyScripter
data = """" <table class="Table 1"> 
<tr>
    <th> Month </th>
    <th> Savings </th>
</tr>
<tr>
    <td> March </td>
    <td> 200 </td>
</tr>
<tr>
    <td> April </td>
    <td> 2001 </td>
</tr>
<tr>
    <td> May </td>
    <td> 2002 </td>
</tr>

</table> """

from bs4 import BeautifulSoup
soup = BeautifulSoup(data, 'lxml')
import pandas as pd

new_tr = soup.new_tag("tr")

#Add as many td (data) you want.
new_td = soup.new_tag('td')
new_td2 = soup.new_tag('td')


new_td.string = "June" # data
new_td2.string = "100"

new_tr.append(new_td)
new_tr.append(new_td2)

#Add whole 'tr'(row) to table.
soup.table.append(new_tr)

df = pd.read_html(str(soup))
print(df)
Answered By: Basavaraj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.