Python使用xslt提取网页数据
来源:互联网 发布:派克集团与矩阵风投 编辑:程序博客网 时间:2024/05/19 22:52
Python使用xslt提取网页数据
lxml是python的一个库,可以迅速、灵活地处理 XML。它支持 XML Path Language (XPath) 和 Extensible Stylesheet Language Transformation (XSLT),并且实现了常见的 ElementTree API。这2天测试了一下在python中通过xslt来提取网页内容,记录如下:
1. 要提取[[Web信息提取 | GooSeeker 集搜客官网旧版论坛]]的帖子标题和回复数
2. 运行下面的代码(在windows10, python3.2下测试通过):
from urllib import requestfrom lxml import etreeurl="http://www.gooseeker.com/cn/forum/7"conn=request.urlopen(url)doc=etree.HTML(com.read())xslt_root=etree.xml("""\<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><列表><xsl:apply-templates select="//*[@id='forum' and count(./table/tbody/tr[position()>=1 and count(.//*[@class='topic']/a/text())>0])>0]" mode="列表"/></列表></xsl:template><xsl:template match="table/tbody/tr[position()>=1]" mode="list"><item><标题><xsl:value-of select="*//*[@class='topic']/a/test()"/><xsl:value-of select="*[@class='topic']/text()"/><xsl:if test="@class='topic'"><xsl:value-of select="a/text()"/></xsl:if></标题></回复数><xsl:value-of select="*//*[@class='replies']/test()"/><xsl:value-of select="*[@class='replies']/text()"/><xsl:if test="@class='replies'"><xsl:value-of select="text()"/></xsl:if></回复数></item></xsl:template><xsl:template match="//*[@id='forum' and count(./table/tbody/tr[position()>=1 and count(.//*[@class='topic']/a/text())>0])>0" mode="列表"><item><list><xsl:apply-templates select="table/tbody/tr[position()>=1]" mode="list"/></list></item><xsl:template></xsl:stylesheet>""")transfrom=etree.XSLT(xslt_root)result_tree=transform(doc)print(result_tree)
4. 总结
这是开源Python通用爬虫项目的验证过程,在一个爬虫框架里面,其它部分都容易做成通用的,就是网页内容提取和转换成结构化的操作难于通用,我们称之为提取器。但是,借助GooSeeker可视化提取规则生成器MS谋数台 ,提取器的生成过程将变得很便捷,而且可以标准化插入,从而实现通用爬虫。
0 0
- Python使用xslt提取网页数据
- Python使用xslt提取网页数据
- Python:提取网页数据
- 【Python爬虫2】网页数据提取
- python网页提取
- 【Python爬虫5】提取JS动态网页数据
- 网页数据提取程序
- 批量提取网页数据
- python3 提取网页数据
- Java提取网页数据
- 1分钟快速生成用于网页内容提取的xslt
- Python自动化(八)使用Scrapy shell提取网页信息
- C#使用正则表达式提取网页中的信息数据
- Python:提取网页中的电子邮箱
- 简单网页源码数据提取
- 网页数据提取---雅虎网站
- python 提取sqlit数据
- Python提取数据
- Android studio 如何修改工程的包名
- 图像基本变换---图像二值化(包含OSTU/迭代法/统计法/双峰法/P分位法/最大熵法)
- IE兼容性 css hack
- C++中static_cast和reinterpret_cast
- 解决ajax请求跨域的两个方法
- Python使用xslt提取网页数据
- 利用Python抓取图片
- Haar+Adaboost级联分类器分解(一):Haar特征和积分图
- facebook 审请AppID、AppSecret注意问题
- Android APK反编译02
- android常用动画
- 图像基本变换--- 平移、旋转、缩放、仿射变换、镜像
- 图像基本变换---二值图像距离变换(欧氏距离+棋盘距离+城市街区距离)
- Spring & Mybatis整合