beautifulsoup简单用法
2017-03-16 12:14
197 查看
原文地址
http://www.cnblogs.com/yupeng/p/3362031.html
这篇文章讲的也很全
http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html
稍微研究了下bs4这个库,运行了下都还好用,就是解析html的各种结构,和xml的elementTree解析库是类似的,使用起来差不多。
可以直接调试,用来熟悉其用法
# coding=utf-8 # 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> """ soup = BeautifulSoup(html_doc,'html.parser') # print soup.title # print soup.title.name # print soup.title.string # print soup.p # print soup.a # print soup.find_all('a') # a=soup.find_all('a') # print len(a) # print soup.find_all('p')#返回类似数组的结构 # p=soup.find_all('p') # print len(p) # print soup.find(id='link3') # print soup.get_text()#返回整个的文本 # print soup.p.get_text()#根据解析的节点来 # for i in soup.find_all('p'): # print i.get_text() # print i.contents # print soup.a['href'],soup.a['class'],soup.a['id'],soup.a.text#注意单节点的每个内容都获取到了 # print soup.html,soup.head,soup.body#s整体,头,身体,全部的结构 # print soup.p.contents,soup.head.contents#列表形式返回子内容 # for i in list(soup.head.children):#不需要知道子节点的名称,迭代遍历子内容 # print i, # print soup.a.parent#向上查找,parents是查找所有的 # for i in soup.html.parents: # print i,len(i) # print soup.a.parent # print soup.find_all(class_="sister") print soup.find_all('a',limit=1)#限制个数
相关文章推荐
- python beautifulsoup简单用法
- python BeautifulSoup的简单用法
- beautifulsoup的简单用法
- python简单爬虫 及 beautifulSoup简单用法
- python爬虫--BeautifulSoup的简单用法
- beautifulsoup的简单用法
- Python:第三方模块BeautifulSoup的安装及简单用法
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单!
- 常用的JS与XML结合用法 简单示例
- zlib用法简单说明
- windows下的CVS最简单的用法
- 常用的JS与XML结合用法 简单示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- oracle 数据库一些单行函数的简单用法
- 时间格式化,简单用法
- ASP.NET中DataGrid的简单用法
- namespace的简单用法
- 枚举简单用法