我正在尝试使用beautifulsoup为来自维基百科的人获取生日。例如,http://en.wikipedia.org/wiki/Ezra_Taft_Benson的生日是1899年8月4日。为了访问bday,我使用以下代码:
bday = url.find("span", class_="bday")
但是,它会选取一个实例,其中bday
作为另一个标记的一部分出现在html代码中。i、 e<span class="bday dtstart published updated">1985-11-10 </span>
。在
有没有办法只与bday
匹配确切的类标记?在
我希望问题是清楚的,因为目前我得到的bday
是1985-11-10,这不是正确的日期。在
当BeautifulGroup的所有其他匹配方法都失败时,您可以使用带单个参数(tag)的函数:
上面搜索的是一个
span
标记,其class属性是单个元素的列表('bday')。在我会这样做的:
这将返回
1899-08-04
作为bday
的值尝试将lxml与
beautifulsoup
解析器一起使用。以下内容只找到<span>
标记,其中只有bday
类(在本页中只有一个):相关问题 更多 >
编程相关推荐