Python轻量级爬虫教程-网页解析器
来源:互联网 发布:网络教育专升本 编辑:程序博客网 时间:2024/06/06 14:22
网页解析器: 从网页中提取我们想要的数据的工具
Python的几种网页解析器:
正则表达式(模糊匹配)
结构化解析:
html.parser
BeautifulSoup(第三方插件)
lxml(第三方插件)
网页解析器之- beautiful Soup
首先测试是否安装beautiful soup4
import bs4
print bs4
如果提示以下表示已经安装了beautiful soup4 ,不然就要自己安装了
<module 'bs4' from 'D:\Python2.7\lib\site-packages\bs4\__init__.pyc'>
Beautiful Soup 语法
#coding:utf8import bs4from bs4 import BeautifulSoup#根据HTML网页字符串创建BeautifulSoup对象soup = BeautifulSoup( html_doc, #HTML文档字符串 'html.parser', #HTML解析器 from_encoding = 'utf8' #HTML文档的编码 )#查找所有标签为a的节点soup.find_all('a')#查找所有标签为a,链接符合/view/123.htm形式的节点soup.find_all('a', href = '/view/123.htm/')soup.find_all('a', href = re.compile(r'/view/\d+\.htm'))#查找所有标签为div, class为abc,文字为Python的节点#class是python的关键字soup.find_all('div', class_='abc', string = 'Python')#得到节点:<a href = '1.html'>Python</a>#获取查找到的节点的标签名称node.name#获取查找到的a节点的href属性node['href']#获取查找到的a节点的链接文字node.get_text()实例代码:
#coding:utf8import bs4import refrom bs4 import BeautifulSouphtml_doc = """<div id="divAll"><div id="divPage"><div id="divMiddle"><div id="divTop"><div id="BlogTitle"><img src="http://www.cnpythoner.com/themes/ecworker/style/default/logo.gif" alt="python" width="230" height="60"></div><div class="banner"><a href="http://www.cnpythoner.com/peixun/info.html"><img src="http://www.cnpythoner.com/images/9354036.gif" alt="python视频教程" usemap="#AV-eggs" border="0" height="70" width="600"></a></div></div><div id="divNavBar"><div class="headLeft"></div><div class="headRight"></div><ul><p class="title"><b>Python教程</b></p><li><a href="http://www.cnpythoner.com/" rel="nofollow">首页</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=11">入门教程</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=4">练习题</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=1">python教程</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=2">django教程</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=15">seo应用</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=16">linux</a></li><li><a href="http://www.cnpythoner.com/catalog.asp?cate=17">测试应用</a></li><li><a href="http://www.cnpythoner.com/pythonbook.html" target="_blank">书籍推荐</a></li>"""#根据HTML网页字符串创建BeautifulSoup对象soup = BeautifulSoup( html_doc, #HTML文档字符串 'html.parser', #HTML解析器 from_encoding = 'utf-8' #HTML文档的编码 )print '获取所有的链接'#查找所有标签为a的节点links = soup.find_all('a')for link in links: print link.name, link['href'], link.get_text()print"获取指定链接"link_node = soup.find('a', href = 'http://www.cnpythoner.com/')print link_node.name, link_node['href'], link_node.get_text()print"正则匹配"link_node = soup.find('a', href = re.compile(r"tal"))print link_node.name, link_node['href'], link_node.get_text()print"获取p段落文字"p_node = soup.find('a',rel="nofollow")print p_node.name, p_node.get_text()
阅读全文
0 0
- Python轻量级爬虫教程-网页解析器
- Python轻量级爬虫教程-网页下载器
- Python爬虫基础-网页解析器-02
- Python爬虫之网页下载器网页解析器
- Python爬虫----网页解析器和BeautifulSoup第三方模块
- python 简单爬虫之网页解析器 beautifusoup4插件应用
- Python 轻量级爬虫
- python爬虫网页解析中的疑难杂症...
- python轻量级爬虫的编写
- <Do it! Python> 轻量级爬虫
- 基于python利用爬虫爬取网页教程
- 基于python利用爬虫爬取网页教程
- 网页爬虫及解析
- 【爬虫】BeautifulSoup解析网页
- 【爬虫】selenium解析网页
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Java中char转string,string转char
- JSP-Java Server Pages(Java服务器页面)<二>
- 数据库设计中常见表结构的设计技巧.
- JAVA 攻城狮 第二十八天
- Select查询语句
- Python轻量级爬虫教程-网页解析器
- fcntl
- CCF201412-4 最优灌溉 就是生成树嘛~
- Linux上安装SmartBI
- android正确使用killProcess完全退出应用
- 从 2017 OpenStack Days China 看国内云计算的发展现状
- IDEA 格式化代码快捷键冲突解决
- android ListView item中添加checkbox之后点击无效的解决办法
- Java开发面试题目