Python中使用BeautifulSoup做网页解析
来源:互联网 发布:网络广告制作软件 编辑:程序博客网 时间:2024/05/01 11:18
BeautifulSoup为第三方库,用于从HTML或XML中提取数据
下载地址:http://www.crummy.com/software/BeautifulSoup/
linux中安装BeautifulSoup:
- 解压后,进入beautifulsoup4-4.3.0目录;
- 在命令行输入
python setup.py install
或者sudo python setup.py install
; - 输入
python
,进入python模块; - 输入
from bs4 import BeautifulSoup
检测是否成功。
'''Created on 2016-4-15https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.html#beautiful-soup-4-2-0@author: developer'''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>"""from bs4 import BeautifulSoupimport re#根据HTML网页字符串创建BeautifulSoup对象'''html_doc #HTML文档字符串'html.parser' #HTML解析器from_encoding='utf-8' #HTML文档的编码'''soup = BeautifulSoup(html_doc, 'html.parser',from_encoding='utf-8')########################################print("获取所有链接")#查找所有标签为a的节点links = soup.find_all('a')#获取查找到的节点的标签名称 node.name#获取查找到的a节点的href属性 node['href']#获取查找到的a节点的链接文字 node.get_text()for link in links: print(link.name, link['href'], link.get_text())#########################################print("获取Lacie的链接")link_node = soup.find('a', href="http://example.com/lacie")print(link_node.name, link_node['href'], link_node.get_text())#########################################print("正则匹配")link_node = soup.find('a', href=re.compile(r"ill"))print(link_node.name, link_node['href'], link_node.get_text())#########################################print("获取p段落文字")p_node = soup.find('p', class_="title") ##class为python中的关键字,所以加了下划线,避免冲突print(p_node.name, p_node.get_text())
0 0
- Python中使用BeautifulSoup做网页解析
- python使用HTMLParser和BeautifulSoup解析网页
- Python 网页解析器BeautifulSoup示例使用
- python使用HTMLParser和BeautifulSoup解析网页
- python 中使用BeautifulSoup
- python : BeautifulSoup 网页HTML 解析器
- python网页解析利器——BeautifulSoup
- python : BeautifulSoup 网页 table 解析范例
- python中BeautifulSoup简单使用
- 【beautifulsoup】python标准库解析器解析网页问题解决
- BeautifulSoup解析网页信息
- beautifulsoup 解析网页内容
- BeautifulSoup解析网页
- 【爬虫】BeautifulSoup解析网页
- 用Python解析HTML,BeautifulSoup使用简介
- Python使用BeautifulSoup解析文档不全
- Python爬虫----网页解析器和BeautifulSoup第三方模块
- python︱HTML网页解析BeautifulSoup学习笔记
- 北邮OJ 1005. 16校赛-Hawei Learning C
- 粗糙的SEM点击率分析模型
- Redis Master/Salve Replication(主从复制模式)
- Linux下C语言获取目录中的文件列表
- 《程序员的思维修炼》读书笔记
- Python中使用BeautifulSoup做网页解析
- SpringMVC注解@RequestParam全面解析
- 分区交换 技术
- android开发中防止刚进入activity时edittext获取焦点,防止自动自动弹出软键盘
- 图解数据结果-复杂度,动态数组和单链表
- 竞争情报分析工具Alexa
- iOS 键盘
- 对iOS runtime methodForSelector 方法的一点理解2
- HTML5技术学习总结