python_BeautifulSoup
来源:互联网 发布:小刀娱乐网源码带采集 编辑:程序博客网 时间:2024/05/29 09:29
博客内容都是经过看视频总结的如果视频中讲师觉得有抄袭,请联系我
Beautiful Soup库的理解:
例子:
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p></p> 标签对,英文名字叫Tag
p 是标签的名字
class=‘’‘title’ 属性Attributes
Beautiful Soup 就是将html 标签树 进行BeautifulSoup 的处理
Beautiful Soup 库的解析器:
bs4的Html 解析器 ,使用的方法 BeautifulSoup(mk,'html.parser') , 条件安装 bs4库
lxml的Html 解析器 ,使用的方法 BeautifulSoup(mk,'lxml') , 条件pip install xml
lxml的xml 解析器 ,使用的方法 BeautifulSoup(mk,'xml') , 条件pip install xml
html5lib的 解析器 ,使用的方法 BeautifulSoup(mk,'html5lib') , 条件pip install html5lib
案例:import requestsfrom bs4 import BeautifulSoup#调用soup库try: r=requests.get("http://python123.io/ws/demo.html",timeout=30) r.raise_for_status() r.encoding=r.apparent_encoding demo=r.text #解析html所以相当于解析demo,使用beautifulSoup解析 soup=BeautifulSoup(demo,'html.parser')#解析 print(soup)except: print("有故障")Beautiful Soup 类的基本元素
Tag 标签,最基本的信息组织单元,分别用<></>标明开头结尾
from bs4 import BeautifulSoupimport requestsr = requests.get("http://python123.io/ws/demo.html", timeout=30)demo=r.textsoup=BeautifulSoup(demo,'html.parser')#打印文本title 标签print(soup.title)#打印文本中的a标签print(soup.a)
Name 标签的名字,<p></p>的名字就是‘p’格式是<tag>.name
from bs4 import BeautifulSoupimport requestsr = requests.get("http://python123.io/ws/demo.html", timeout=30)demo=r.textsoup=BeautifulSoup(demo,'html.parser')#分别打印了a标签的name,a标签他爸的name,a标签他祖宗的nameprint(soup.a.name)print(soup.a.parent.name)print(soup.a.parent.parent.name)Attributes 标签的属性,字典形式组织,格式<tag>.attrs
from bs4 import BeautifulSoupimport requestsr = requests.get("http://python123.io/ws/demo.html", timeout=30)demo=r.textsoup=BeautifulSoup(demo,'html.parser')#标签的属性 先告诉那个标签的什么属性tag=soup.a#a标签下的属性<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">print(tag.attrs)#打印结果{'href': 'http://www.icourse163.org/course/BIT-268001', 'id': 'link1', 'class': ['py1']print(tag.attrs.values())#字典打印对应的kv 值NavigableString 标签内的非属性字符串,<>.......<>格式<tag>.string
print(soup.a.string)Comment 标签内的注释部分格式<tag>.string
- python_BeautifulSoup
- python_BeautifulSoup库之过滤器
- Python_BeautifulSoup 抓取网页内容入门
- Python_BeautifulSoup 抓取网页内容入门
- Python_BeautifulSoup 抓取网页内容入门
- python_BeautifulSoup库之遍历文档树
- 工作语言http服务器工具框架数据库
- Linux常用命令
- 一个简单地聊天程序
- SpringMVC @RequestParam 中文乱码问题解决
- spring原理
- python_BeautifulSoup
- 单片机时钟程序(6)key.c
- Promise项目实践与异常处理方式
- 面对offer,如何选择
- javascript手册地址
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- 位运算
- PyCharm下载地址
- spring-data-mongo 实现aggregation