#coding:utf8 from bs4 import BeautifulSoup #指定要搜索的文档: html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') # 传入文档 解析器 指定编码:UTF-8 soup=BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8') links=soup.find_all('a') print '获取 P 段落的文字' link_node=soup.find('p' ,class_="title") #不能用class,因为跟python关键字冲突 print link_node.name,link_node.get_text() #输出: ''' 获取 P 段落的文字 p The Dormouse's story '''
python之find all
最新推荐文章于 2024-05-07 09:59:10 发布