簡體   English   中英

Python Beautifulsoup解析html

[英]Python Beautifulsoup parse html

我正在嘗試使用BeautifulSoup4解析html:

<tr  class="odd" >
<td><a href="show_result.php?id=7084083" title="Show the User ID DB records for the id '7084083'"  tabindex="5" >7084083</A></td>
<td><a href="show_result.php?name=bernd" title="Show the User ID DB records the name 'bernd'"   >bernd</A></td>
<td><a href="show_result.php?range=DDF+User" title="range_link"   >DDF User</A></td>
<td>mandatory</td>
<td>Solaris</td>
<td>valid</td>
<!-- xxxx old style  -->
<!-- xxxx showdetail navlink -->
<td><a class="navlink" href="show_detail.php?rec_id=283330130"  title="show the detail for this entry [alt-E]" accesskey="E"><img src="detail.gif" alt="show the detail for this entry [alt-E]" title="show the detail for this entry [alt-E]" border="0">&nbsp;</a></td>
</tr>

我想過濾掉第一個“ id = 7084083” =>(7084083)

由於您正在搜索html的特定部分,因此使用re代替bs4可能更容易:

import re
s = """
<tr  class="odd" >
<td><a href="show_result.php?id=7084083" title="Show the User ID DB records for the id '7084083'"  tabindex="5" >7084083</A></td>
<td><a href="show_result.php?name=bernd" title="Show the User ID DB records the name 'bernd'"   >bernd</A></td>
<td><a href="show_result.php?range=DDF+User" title="range_link"   >DDF User</A></td>
<td>mandatory</td>
<td>Solaris</td>
<td>valid</td>
<!-- xxxx old style  -->
<!-- xxxx showdetail navlink -->
<td><a class="navlink" href="show_detail.php?rec_id=283330130"  title="show the detail for this entry [alt-E]" accesskey="E"><img src="detail.gif" alt="show the detail for this entry [alt-E]" title="show the detail for this entry [alt-E]" border="0">&nbsp;</a></td>
</tr>
"""
final_id = re.findall('(?<=id\=)\d+', s)[0]

輸出:

'7084083'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM