Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Soup.find()方法制作精美的汤拼图_Python_Html_Beautifulsoup_Find - Fatal编程技术网

Python 使用Soup.find()方法制作精美的汤拼图

Python 使用Soup.find()方法制作精美的汤拼图,python,html,beautifulsoup,find,Python,Html,Beautifulsoup,Find,我正在学习Beautiful Soup和Python,在此背景下,我正在使用包含不同年份流行婴儿名字(例如baby1990.html等)的html文件集,在Regex上进行Google教程中的“婴儿名字”练习。如果您感兴趣,可以在此处找到此数据集: html文件包含一个特定的表,该表存储流行的婴儿姓名,其html代码如下: <table width="100%" border="0" cellspacing="0" cellpadding="4" summary="formatting"&

我正在学习Beautiful Soup和Python,在此背景下,我正在使用包含不同年份流行婴儿名字(例如baby1990.html等)的html文件集,在Regex上进行Google教程中的“婴儿名字”练习。如果您感兴趣,可以在此处找到此数据集:

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 />
&nbsp; Select another <label for="yob">year of birth</label>?<br />      
<form method="post" action="/cgi-bin/popularnames.cgi">
&nbsp; <input type="text" name="year" id="yob" size="4" value="1990">
<input type="hidden" name="top" value="1000">
<input type="hidden" name="number" value="">
&nbsp; <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...
但是,此命令无法选择目标表。 相反,以下命令成功执行:

table = soup.find(summary="Popularity for top 1000")
您能否通过查看html代码来解释为什么第一个命令失败,第二个命令成功

非常感谢您的建议。

我刚才回答了你的问题,代码是有效的


还有一件事,
html.patser
在python2中被破坏了,不要使用它,使用
lxml

你在解析吗?@alecxe
请求。get()
对该url失败。知道为什么吗?@MYGz你得到了什么?(对我来说是有效的)这可能是国家限制吗?(因为它是美国的SSA)@alecxe它在工作。我猜是打字错误。太好了!它终于奏效了。非常感谢!但是,请告诉我为什么您的命令使用summary=“”Popularity for top 1000”,因为summary=“formatting”。我不明白这一点。@im7有两个标记包含
summary
,只需搜索HTML代码即可
right_table = soup.find("table", summary = "formatting")
table = soup.find(summary="Popularity for top 1000")