原文地址

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. SVN Server 500 NotLicensed 错误的解决方法

    SVN Server 500 NotLicensed 错误的HTML页面显示 Not licensed The server encountered an internal error or misc ...

  2. opencv2 直方图之calchist函数使用(转)

    OpenCV提供了calcHist函数来计算图像直方图. 其中C++的函数原型如下:void calcHist(const Mat* arrays, int narrays, const int* c ...

  3. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  4. SQL Server清理Log文件

    ---SQL 2008 在SQL2008中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式. USE [master] go ALTER DATABASE PS SET RECOVERY ...

  5. 应用maven自动部署的脚本

    @(编程) 最近写了一个自动部署的脚本,可以一键部署到测试服务器或者生产服务器上,包括一个函数脚本和一个调用脚本,比较简单,记录如下. 特点如下: 部署前自动备份 可以部署tomcat项目和java项 ...

  6. Linux centos7下设置Tomcat开机自启动

    1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...

  7. 2-sat基础题 uvalive 3211

    蓝书325页的基础题 二分+2-sat //看看会不会爆int!数组会不会少了一维! //取物问题一定要小心先手胜利的条件 #include <bits/stdc++.h> using n ...

  8. [转] Linux下程序的加载、运行和终止流程

    TAG: linux, main, _start DATE: 2013-08-08 原文地址: http://blog.csdn.net/tigerscorpio/article/details/62 ...

  9. TensorFlow下利用MNIST训练模型识别手写数字

    本文将参考TensorFlow中文社区官方文档使用mnist数据集训练一个多层卷积神经网络(LeNet5网络),并利用所训练的模型识别自己手写数字. 训练MNIST数据集,并保存训练模型 # Pyth ...

  10. matlab求逆运算:左除( \ )和右除( / ),inv,pinv

    矩阵求逆可以使用左除(\)和右除(/),inv,pinv 首先了解需要求逆的矩阵A是否为奇异方阵 inv 若A为非奇异方阵,则存在逆矩阵,可利用inv求逆: inv(A) pinv 若需要求逆的矩阵A ...