原文地址

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)#限制个数

beautifulsoup简单用法的更多相关文章

  1. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  2. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  3. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  4. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  5. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  6. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  7. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

随机推荐

  1. Qt ------ QString 操作

    QStringList QString::arg     ------- 字符串的格式化处理,类始于sprintf 比如:QString("%1").arg(10,2,16,QLa ...

  2. LINUX安全加固操作

    1.禁止Ctrl-Alt-Delete组合键重启系统 vi /etc/inittab #ca::ctrlaltdel:/sbin/shutdown -t3 -r now 如果还存在下面的文件,则需要注 ...

  3. ubuntu如何杀死进程

    一.得到所有进程 先用命令查询出所有进程 ps -ef 二.杀死进程 我们使用ps -ef命令之后,就会得到一些列进程信息,有进程pid什么的,如果你要杀死莫个进程的话,直接使用命令   kill   ...

  4. sql like参数化查询

    如下例所示,这样做了后再也不担心sql注入了.... public static DataTable GetProPriEEfocusNew(string ProName) { StringBuild ...

  5. TypeError: only integer scalar arrays can be converted to a scalar index

    TypeError: only integer scalar arrays can be converted to a scalar index 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...

  6. [USACO07FEB] Lilypad Pond

    https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...

  7. 给APP增加RSA签名

    RSA签名,Google主要用于APP的来源控制与结算.所谓的结算,也是就是控制了APP只有使用现在机子上登录的Google账户从Google市场曾经下载过该APP的才能够使用,这样也就达到了app销 ...

  8. IIC总线学习

    IIC总线 IIC协议简要说明: 1.2条双向串行线,一条数据线称为SDA,一条时钟线SCL,双向半双工 2.传输的设备之间只是简单的主从关系,主机可以作为主机发送也可以作为主机接收,任何时候只能由一 ...

  9. 【Atcoder】AGC 020 B - Ice Rink Game 递推

    [题意]n个人进行游戏,每轮只保留最大的a[i]倍数的人,最后一轮过后剩余2人,求最小和最大的n,或-1.n<=10^5. [算法]递推||二分 [题解]令L(i),R(i)表示第i轮过后的最小 ...

  10. js关闭当前页面跳转新页面

    页面代码: <p class="info"><span style="font-weight: bold">所属项目:</span ...