[英]Why is Beautiful Soup's soup.find() bringing me empty content?
[英]Beautiful Soup puzzle with soup.find() method
我正在學習Beautiful Soup和Python,在這種情況下,我正在使用包含不同年份流行的嬰兒名字的html文件集(例如baby1990.html等)在Regex上進行Google教程的“嬰兒名字”練習。 如果您對此感興趣,可以找到此數據集: https : //developers.google.com/edu/python/exercises/baby-names
html文件包含一個特定的表,該表存儲了常用的嬰兒名字,並且其html代碼如下:
<table width="100%" border="0" cellspacing="0" cellpadding="4" summary="formatting">
<tr valign="top"><td width="25%" class="greycell">
<a href="../OACT/babynames/background.html">Background information</a>
<p><br />
Select another <label for="yob">year of birth</label>?<br />
<form method="post" action="/cgi-bin/popularnames.cgi">
<input type="text" name="year" id="yob" size="4" value="1990">
<input type="hidden" name="top" value="1000">
<input type="hidden" name="number" value="">
<input type="submit" value=" Go "></form>
</td><td>
<h3 align="center">Popularity in 1990</h3>
<p align="center">
<table width="48%" border="1" bordercolor="#aaabbb"
cellpadding="2" cellspacing="0" summary="Popularity for top 1000">
<tr align="center" valign="bottom">
<th scope="col" width="12%" bgcolor="#efefef">Rank</th>
<th scope="col" width="41%" bgcolor="#99ccff">Male name</th>
<th scope="col" bgcolor="pink" width="41%">Female name</th></tr>
<tr align="right"><td>1</td><td>Michael</td><td>Jessica</td> # Targeted row
<tr align="right"><td>2</td><td>Christopher</td><td>Ashley</td> # Targeted row
etc...
html文件中還有一個我不想捕獲的表,它具有以下html代碼。
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<tbody>
<tr><td class="sstop" valign="bottom" align="left" width="25%">
Social Security Online
</td><td valign="bottom" class="titletext">
<!-- sitetitle -->Popular Baby Names
</td>
</tr>
<tr bgcolor="#333366"><td colspan="2" height="2"></td></tr>
<tr><td class="graystars" width="25%" valign="top">
<a href="../OACT/babynames/">Popular Baby Names</a></td><td valign="top">
<a href="http://www.ssa.gov/"><img src="/templateimages/tinylogo.gif"
width="52" height="47" align="left"
alt="SSA logo: link to Social Security home page" border="0"></a><a name="content"></a>
<h1>Popular Names by Birth Year</h1>September 12, 2007</td>
</tr>
<tr bgcolor="#333366"><td colspan="2" height="1"></td></tr>
</tbody></table>
在比較兩個表的表標簽時,我得出結論,目標表(我要捕獲的表)的獨特特征是“摘要”屬性,該屬性似乎具有“格式”值。 因此,我嘗試了以下命令:
right_table = soup.find("table", summary = "formatting")
但是,此命令無法選擇目標表。 相反,以下命令成功執行:
table = soup.find(summary="Popularity for top 1000")
您能否通過查看html代碼來解釋為什么第一個命令失敗而第二個命令成功?
您的建議將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.