Beautiful的用法
来源:互联网 发布:淘宝网宝宝学步鞋 编辑:程序博客网 时间:2024/06/05 21:16
<pre name="code" class="python"># -*- coding: utf-8 -*-"""Created on Wed Sep 30 15:36:15 2015@author: tanyouwei"""from bs4 import BeautifulSouphtml_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><b>The Dormouse's story</b> <p class="story">Once upon a time there were three little sisters; and their names were<a href="http://www.jb51.net" class="sister" id="link1">Elsie</a>,<a href="http://www.jb51.net" class="sister" id="link2">Lacie</a> and<a href="http://www.jb51.net" class="sister" id="link3">Tillie</a>;and they lived at the bottom of a well. <p class="story">... """soup = BeautifulSoup(html_doc) #soup就是BeautifulSoup处理格式化后的字符串print soup.title #得到title标签print soup.title.nameprint soup.title.stringprint soup.p #得到的是文档中的第一个p标签,要想得到所有标签,得用find_all函数print soup.a #获得第一个a标签print soup.a['href'] #获得第一个a标签的href属性的值print soup.a['class'] #获得第一个a标签的class属性的值 print soup.a['id'] #获得第一个a标签的class属性的值 print soup.head #一些特殊的标签,比如head标签,是可以通过soup.head得到print soup.body.contents #获得body下的所有子孩子,以列表的形式返回结果, #可以使用[num]的形式获得,获得标签,使用.name就可以print soup.find_all('a') #find_all函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.print soup.find_all('p','title')print soup.find_all('a','sister')print soup.find_all('a',class_='sister')print soup.find(id='link3')print soup.get_text() #返回所有的文本print soup.p.get_text() #返回第一个p标签的文本'''通过css查找'''print soup.select("p.title")print soup.select("p.story")'''通过属性进行查找'''print soup.find_all("a", attrs={"class": "sister"})print soup.find_all("a", {"class", "sister"})'''通过文本进行查找'''print soup.find_all(text="Elsie")print soup.find_all(text=["Tillie", "Elsie", "Lacie"])'''限制结果个数'''print soup.find_all("a", limit=2)
0 0
- Beautiful的用法
- Beautiful Soup的用法
- Beautiful Soup 的用法
- Beautiful Soup的用法
- python Beautiful Soup的用法
- Python爬虫入门-Beautiful Soup的用法
- Python爬虫利器Beautiful Soup的用法
- python beautiful soup库的用法
- 六.Python爬虫Beautiful Soup的用法
- Python爬虫入门八之Beautiful Soup的用法
- Python爬虫入门之Beautiful Soup的用法
- Python爬虫入门八之Beautiful Soup的用法
- Python爬虫入门八之Beautiful Soup的用法
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门(8):Beautiful Soup的用法
- Python爬虫入门八之Beautiful Soup的用法
- Python爬虫利器二之Beautiful Soup的用法
- Python爬虫利器二之Beautiful Soup的用法
- 通过反射机制获得系统字体Size【ActivityManagerNative无法调用问题】
- iOS中CoreLocation定位的代理方法不执行的解决办法。
- ListView设置滑动到底部自动加载 重复多次加载问题
- dojo 1.10 学习日记-Hello Dojo!
- acm-最小生成树(prim)
- Beautiful的用法
- UITableView全面讲解
- oracle数据库的多行函数
- C# Window编程随记——读取.txt文件内容
- MakeFile
- Eclipse内存配置
- [转]Redis 的 Sentinel
- 数据库sql语句优化方案
- AIX查看CPU及内存参数