beautifulsoup简单用法
原文地址
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简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
随机推荐
- wx.showToast 延时跳转~~~
//提交预约订单 wx.request({ url: 'http://www.pusonglin.cn/app/index.php?i=2&c=entry&do=api&op= ...
- \G,sql中select 如果太长,可以在后面放\G,竖行显示~~~~
1.使用\G按行垂直显示结果 如果一行很长,需要这行显示的话,看起结果来就非常的难受. 在SQL语句或者命令后使用\G而不是分号结尾,可以将每一行的值垂直输出. mysql> select * ...
- eclipse+myeclipse 使用技巧备忘
myeclipse 导入多模块maven项目 https://blog.csdn.net/jack85986370/article/details/51371853 maven项目在eclipse的l ...
- 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域
在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...
- UVA 11982 Fantasy Cricket
https://vjudge.net/problem/UVA-11982 题意: 给出一个包含’U’, ‘D’, ‘E’的字符串, ’U’ 表示需要把这个字符向后移动, ’D’表示需要把这个字符向前移 ...
- 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D
http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...
- HDU 2619 完全剩余类 原根
求有多少$i(<=n-1)$,使 $x^i \mod n$的值为$[1,n-1]$,其实也就是满足完全剩余类的原根数量.之前好像在二次剩余的讲义PPT里看到这个过. 直接有个定理,如果模k下有 ...
- Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律
Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...
- JAVA多线程提高三:线程范围内共享变量&ThreadLocal
今天我们学习的是如何在线程自己的范围内达到变量数据的共享,而各个线程之间又是互相独立开来,各自维护的,即我们说的ThreadLocal的作用. 一.概念 可以将每个线程用到的数据与对应的线程号存放到一 ...
- 分享一个数据库sql_mode 引起的坑
sql_mode坑 MySQL 5.7.x 默认值: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR ...