python 使用re实现xpath节点功能
来源:互联网 发布:常熟淘宝培 编辑:程序博客网 时间:2024/04/28 11:39
#! /usr/bin/python# #encoding:utf-8import re#!/usr/bin/python#encoding:utf-8import redef foo(xpath, content): print xpath if xpath.startswith('/'): arr= xpath[1:].split('/', 1) node = arr[0] c = re.compile(r"\<%s\>(.*?)\<\/%s\>" % (node, node), re.S) # print c.pattern content_list = c.findall(content) if len(arr) > 1: xpath = '/' + arr[1] return foo(xpath, content_list[0].strip()) #注意这里一定要加上return返回值 else: return content_list[0].strip()s = '''<html> <header><title>hello world</title></header> <body> <div> <h1>Hello World</h1> </div> <div> <span>test</span> </div> </body> </html>'''xpath = '/html/body/span'print foo(xpath, s)
抓取豆瓣最佳影评
import reimport codecsdef foo(xpath,content): xpath=xpath[1:] arr=xpath.split('/',1) cn_pattern = re.compile('[^\x00-\xff]+.*?[^\x00-\xff]+') if len(arr)>0: node=arr[0] fd_pattern=re.compile(r"\<%s(.*?)\<\/%s\>" %(node,node),re.S) fd_result=fd_pattern.findall(content) if len(arr)>1: xpath='/'+arr[1] for i in range(len(fd_result)): foo(xpath, fd_result[i]) else: print cn_pattern.search(fd_result[0].strip()).group()f=codecs.open('D:/doubanhtml/douban0.html','r','utf-8')content=f.read()xpath=('/header/h3/a')foo(xpath,content)
0 0
- python 使用re实现xpath节点功能
- 使用 XPath 导航选择节点
- 使用 XPath 导航选择节点
- Python re 模块使用
- python中使用XPath
- python---xpath使用
- python中使用XPath
- 使用 Python 模块 re 实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- 使用 Python 模块 re 实现解析小工具
- Dom4J使用XPath功能注意事项
- python使用lxml解析html获取页面内所有叶子节点的xpath路径
- java中实现通过xpath过滤节点
- XPath 节点
- XPath 节点
- XPath 节点
- XPath 节点
- R48-
- 51Nod-区间中的最大数(RMQ)
- 面向对象的理解
- 快速幂取余算法
- 一些lora server开源平台
- python 使用re实现xpath节点功能
- Android探索之旅(第二篇) App启动过程
- 第四周项目6—— 多项式求和
- 第25周-window程序设计(基础篇)-第3章-注册窗口类别2
- [Leetcode] Binary Search Tree Iterator
- 欢迎使用CSDN-markdown编辑器
- 有类似范、36、瘾技的外网推荐一下么?
- 336. Palindrome Pairs
- 数据库中的事务的特性和隔离级别 MindJet整理