0

0

怎么使用Python中的正则表达式处理html文件

WBOY

WBOY

发布时间:2023-05-17 22:35:47

|

1682人浏览过

|

来源于亿速云

转载

使用python中的正则表达式处理html文件

finditer方法是一种全匹配方法。已经使用过findall方法的话,该方法将返回由多个匹配字符串组成的列表。对于多个匹配项,finditer会按顺序返回一个迭代器,每个迭代生成一个匹配对象。这些匹配对象可通过for循环访问,在下面的代码中,因此组1可以被打印。

您需要撰写 Python 正则表达式,以便在 HTML 文本文件中识别特定的模式。将代码添加到STARTER脚本为这些模式编译RE(将它们分配给有意义的变量名称),并将这些RE应用于文件的每一行,打印出找到的匹配项。

1.编写识别HTML标签的模式,然后将其打印为“TAG:TAG string”(例如“TAG:b”代表标签)。为了简单起见,假设左括号和右括号每个标记的()将始终出现在同一行文本中。第一次尝试可能使regex“<.>”其中“.”是与任何字符匹配的预定义字符类符号。尝试找出这一点,找出为什么这不是一个好的解决方案。编写一个更好的解决方案,解决这个问题

2.修改代码,使其区分开头和结尾标记(例如p与/p)打印OPENTAG和CLOSETAG

立即学习Python免费学习笔记(深入)”;

import sys, re

#------------------------------

testRE = re.compile('(logic|sicstus)', re.I)
testI = re.compile('<[A-Za-z]>', re.I)
testO = re.compile('<[^/](\S*?)[^>]*>')
testC = re.compile(']*>')

with open('RGX_DATA.html') as infs: 
    linenum = 0
    for line in infs:
        linenum += 1
        if line.strip() == '':
            continue
        print('  ', '-' * 100, '[%d]' % linenum, '\n   TEXT:', line, end='')
    
        m = testRE.search(line)
        if m:
            print('** TEST-RE:', m.group(1))

        mm = testRE.finditer(line)
        for m in mm:
            print('** TEST-RE:', m.group(1))
        
        index= testI.finditer(line)
        for i in index:
           print('Tag:',i.group().replace('<', '').replace('>', ''))
           
        open1= testO.finditer(line)
        for m in open1:
           print('opening:',m.group().replace('<', '').replace('>', ''))
           
        close1= testC.finditer(line)
        for n in close1:
           print('closing:',n.group().replace('<', '').replace('>', ''))

请注意,有些HTML标签有参数,例如:

慧中标AI标书
慧中标AI标书

慧中标AI标书是一款AI智能辅助写标书工具。

下载

成功查找到并打印标记标签,确保启用带参数和不带参数的标记模式。现在扩展您的代码,以便打印两个打开的标签标签和参数,例如:

OPENTAG: tablePARAM: border=1PARAM: cellspacing=0PARAM: cellpadding=8
 		open1= testO.finditer(line)
        for m in open1:
            #print('opening:',m.group().replace('<', '').replace('>', ''))
            firstm= m.group().replace('<', '').replace('>', '').split()
            num = 0
            for otherm in firstm:
                if num == 0:
                    print('opening:',otherm)
                else:
                    print('pram:',otherm)
                num+= 1

在正则表达式中,可以使用反向引用来指示匹配早期部分的子字符串,应再次出现正则表达式的。格式为\N(其中N为正整数),并返回到第N个匹配的文本正则表达式组。例如,正则表达式,如:r" (\w+) \1 仅当与组(\w+)完全匹配的字符串再次出现时才匹配 backref\1出现的位置。这可能与字符串“踢”匹配.例如,“the”出现两次。使用反向引用编写一个模式,当一行包含成对的open和关闭标签,例如在粗体中.

考虑到我们可能想要创建一个执行HTML剥离的脚本,即一个HTML文件,并返回一个纯文本文件,所有HTML标记都已从中删除出来这里我们不打算这样做,而是考虑一个更简单的例子,即删除我们在输入数据文件的任何行中找到的HTML标记。

如果您已经定义了一条RE来识别HTML标签,您应该可以将生成的文本输出为STRIPPED,并将其打印在屏幕上。。

import sys, re

#------------------------------
# PART 1: 

   # Key thing is to avoid matching strings that include
   # multiple tags, e.g. treating '

' as a single # tag. Can do this in several ways. Firstly, use # non-greedy matching, so get shortest possible match # including the two angle brackets: tag = re.compile('') # The above treats the '/' of a close tag as a separate # optional component - so that this doesn't turn up as # part of the match '.group(1)', which is meant to return # the tag label. # Following alternative solution uses a negated character # class to explicitly prevent this including '>': tag = re.compile(']+)>') # Finally, following version separates finding the tag # label string from any (optional) parameters that might # also appear before the close angle bracket: tag = re.compile(r']+)?>') # Note that use of '\b' (as word boundary anchor) here means # we must mark the regex string as a 'raw' string (r'..'). #------------------------------ # PART 2: # Following closeTag definition requires first first char # after the open angle bracket to be '/', while openTag # definition excludes this by requiring first char to be # a 'word char' (\w): openTag = re.compile(r'<(\w[^>]*)>') closeTag = re.compile(r']*)>') # Following revised definitions are more carefully stated # for correct extraction of tag label (separately from # any parameters: openTag = re.compile(r'<(\w+\b)([^>]+)?>') closeTag = re.compile(r'') #------------------------------ # PART 3: # Above openTag definition will already get the string # encompassing any parameters, and return it as # m.group(2), i.e. defn: openTag = re.compile(r'<(\w+\b)([^>]+)?>') # If assume that parameters are continuous non-whitespace # chars separated by whitespace chars, then we can divide # them up using split - and that's how we handle them # here. (In reality, parameter strings can be a lot more # messy than this, but we won't try to deal with that.) #------------------------------ # PART 4: openCloseTagPair = re.compile(r'<(\w+\b)([^>]+)?>(.*?)') # Note use of non-greedy matching for the text falling # *between* the open/close tag pair - to avoid false # results where have two similar tag pairs on same line. #------------------------------ # PART 5: URLS # This is quite tricky. The URL expressions in the file # are of two kinds, of which the first is a string # between double quotes ("..") which may include # whitespace. For this case we might have a regex: url = re.compile('href=("[^">]+")', re.I) # The second case does not have quotes, and does not # allow whitespace, consisting of a continuous sequence # of non-whitespace material (that ends when you reach a # space or close bracket '>'). This might be: url = re.compile('href=([^">\s]+)', re.I) # We can combine these two cases as follows, and still # get the expression back as group(1): url = re.compile(r'href=("[^">]+"|[^">\s]+)', re.I) # Note that I've done nothing here to exclude 'mailto:' # links as being accepted as URLS. #------------------------------ with open('RGX_DATA.html') as infs: linenum = 0 for line in infs: linenum += 1 if line.strip() == '': continue print(' ', '-' * 100, '[%d]' % linenum, '\n TEXT:', line, end='') # PART 1: find HTML tags # (The following uses 'finditer' to find ALL matches # within the line) mm = tag.finditer(line) for m in mm: print('** TAG:', m.group(1), ' + [%s]' % m.group(2)) # PART 2,3: find open/close tags (+ params of open tags) mm = openTag.finditer(line) for m in mm: print('** OPENTAG:', m.group(1)) if m.group(2): for param in m.group(2).split(): print(' PARAM:', param) mm = closeTag.finditer(line) for m in mm: print('** CLOSETAG:', m.group(1)) # PART 4: find open/close tag pairs appearing on same line mm = openCloseTagPair.finditer(line) for m in mm: print("** PAIR [%s]: \"%s\"" % (m.group(1), m.group(3))) # PART 5: find URLs: mm = url.finditer(line) for m in mm: print('** URL:', m.group(1)) # PART 6: Strip out HTML tags (note that .sub will do all # possible substitutions, unless number is limited by count # keyword arg - which is fortunately what we want here) stripped = tag.sub('', line) print('** STRIPPED:', stripped, end = '')

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

9

2026.01.21

C++多线程相关合集
C++多线程相关合集

本专题整合了C++多线程相关教程,阅读专题下面的的文章了解更多详细内容。

3

2026.01.21

无人机驾驶证报考 uom民用无人机综合管理平台官网
无人机驾驶证报考 uom民用无人机综合管理平台官网

无人机驾驶证(CAAC执照)报考需年满16周岁,初中以上学历,身体健康(矫正视力1.0以上,无严重疾病),且无犯罪记录。个人需通过民航局授权的训练机构报名,经理论(法规、原理)、模拟飞行、实操(GPS/姿态模式)及地面站训练后考试合格,通常15-25天拿证。

13

2026.01.21

Python多线程合集
Python多线程合集

本专题整合了Python多线程相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.21

java多线程相关教程合集
java多线程相关教程合集

本专题整合了java多线程相关教程,阅读专题下面的文章了解更多详细内容。

3

2026.01.21

windows激活码分享 windows一键激活教程指南
windows激活码分享 windows一键激活教程指南

Windows 10/11一键激活可以通过PowerShell脚本或KMS工具实现永久或长期激活。最推荐的简便方法是打开PowerShell(管理员),运行 irm https://get.activated.win | iex 脚本,按提示选择数字激活(选项1)。其他方法包括使用HEU KMS Activator工具进行智能激活。

2

2026.01.21

excel表格操作技巧大全 表格制作excel教程
excel表格操作技巧大全 表格制作excel教程

Excel表格操作的核心技巧在于 熟练使用快捷键、数据处理函数及视图工具,如Ctrl+C/V(复制粘贴)、Alt+=(自动求和)、条件格式、数据验证及数据透视表。掌握这些可大幅提升数据分析与办公效率,实现快速录入、查找、筛选和汇总。

6

2026.01.21

毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm
毒蘑菇显卡测试网站入口 毒蘑菇测试官网volumeshader_bm

毒蘑菇VOLUMESHADER_BM测试网站网址为https://toolwa.com/vsbm/,该平台基于WebGL技术通过渲染高复杂度三维分形图形评估设备图形处理能力,用户可通过拖动彩色物体观察画面流畅度判断GPU与CPU协同性能;测试兼容多种设备,但中低端手机易卡顿或崩溃,高端机型可能因发热降频影响表现,桌面端需启用独立显卡并使用支持WebGL的主流浏览器以确保准确结果

15

2026.01.21

github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

7

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Bootstrap 5教程
Bootstrap 5教程

共46课时 | 2.9万人学习

AngularJS教程
AngularJS教程

共24课时 | 2.8万人学习

CSS教程
CSS教程

共754课时 | 21.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号