一.
二. select
三. find和find_all
tgt=soup.find_all('li',attrs={'class':'clearfix'})
1. 'li'表示要定位的标签;'class'表示标签‘li’的属性,‘clearfix’表示标签‘li’中属性‘class’的属性值。
①函数会首先定位标签li,然后匹配标签li内的class属性值。此处是模糊匹配,即属性class的值为‘xxxclearfixxxx’也符合匹配要求。
②find返回匹配的第一个对象,find_all返回匹配的所有对象。
2.find_all的返回值类型是list。
for item in tgt:
print(type(item))
print(item['href'])
print(item.text)
①item的形式应当如下,只有一个类型的标签,此处为‘a’:
<a href="http://stock.finance.sina.com.cn/hkstock/go/CompanyNoticeDetail/code/09939/aid/1006221.html" target="_blank">公告 内幕消息 普克鲁胺单药于中国III期...</a>
②当item的类型为<class 'bs4.element.Tag'>:
item['href']得到标签a内的href属性值如下:http://stock.finance.sina.com.cn/hkstock/go/CompanyNoticeDetail/code/09939/aid/1006221.html
item.text得到标签的内容如下:
公告 内幕消息 普克鲁胺单药于中国III期...
该博客介绍了如何利用Python的BeautifulSoup库来解析HTML文档,特别是找到所有class为'clearfix'的li元素。通过find_all方法,可以获取到这些元素,并进一步提取出a标签内的href属性和文本内容。内容涉及网页抓取与数据分析的基础技巧。
2200

被折叠的 条评论
为什么被折叠?



