【问题标题】:Python regular expression match in html filehtml文件中的Python正则表达式匹配
【发布时间】:2016-08-08 06:09:29
【问题描述】:

我正在尝试在 html 文件中进行匹配。这是html:

<td>
<b>BBcode</b><br />
<textarea onclick='this.select();' style='width:300px;     height:200px;' />
[URL=http://someimage.com/LwraZS1]          [IMG]http://t1.someimage.com/LwraZS1.jpg[/IMG][    [/URL] [URL=http://someimage.com/CDnuiST]   [IMG]http://t1.someimage.com/CDnuiST.jpg[/IMG]   [/URL] [URL=http://someimage.com/Y0oZKPb][IMG]http://t1.someimage.com/Y0oZKPb.jpg[/IMG][/URL] [URL=http://someimage.com/W2RMAOR][IMG]http://t1.someimage.com/W2RMAOR.jpg[/IMG][/URL] [URL=http://someimage.com/5e5AYUz][IMG]http://t1.someimage.com/5e5AYUz.jpg[/IMG][/URL] [URL=http://someimage.com/EWDQErN][IMG]http://t1.someimage.com/EWDQErN.jpg[/IMG][/URL]
</textarea>
</td>

我想提取从 [ to ] 包含的所有 BB 代码。

这是我的代码:

import re
x = open('/xxx/xxx/file.html', 'r').read
y = re.compile(r"""<td> <b>BBcode</b><br /><textarea onclick='this.select();' style='width:300px; height:200px;' />. (. *) </textarea> </td>""") 
z  = y.search(str(x())
print z          

但是当我运行它时,我得到了 None 对象......错误在哪里?

【问题讨论】:

  • 忘记了read()
  • 什么都没有,仍然得到一个 None.. 可能正则表达式是错误的..
  • 是的,发布了一个 answer.check。

标签: python html regex


【解决方案1】:
import re
x = open('/xxx/xxx/file.html', 'rt').read()
r1 = r'<textarea.*?>(.*?)</textarea>'
s1 = re.findall(r1, s, re.DOTALL)[1] # just by inspection
r2 = r'\[(.*?)\]'
s2 = re.findall(r2, s1)
for u in s2:
    print(u)

【讨论】:

  • 感谢它的工作,但它得到了 html 的另一部分,因为它的全部在
  • @AndrewStef 你能显示一些预期的输出吗?在正则表达式问题中它总是有帮助的。
  • 预期输出应该是 [URL=someimage.com/LwraZS1] [IMG]t1.someimage.com/LwraZS1.jpg[/IMG][ [/URL] [URL=someimage.com/CDnuiST] [IMG]t1.someimage.com/CDnuiST.jpg [/IMG][/URL].. .这正是。该页面是 someimage.com 上传文件的输出。我正在尝试从中捕获 [ 和 ] 文本之间的 BBCODE。
  • @AndrewStef 试试y = re.compile(r'&lt;textarea.*?&gt;(?:\[(.*?)\])*?&lt;/textarea&gt;', re.DOTALL)z = y.findall(x)
  • 找不到匹配项.. 不幸的是
【解决方案2】:

我会为此使用解析器:

from html import HTMLParser

class MyHtmlParser(HTMLParser):
    def __init__(self):
        self.reset()
        self.convert_charrefs = True
        self.dat = []
    def handle_data(self, d):
        self.dat.append(d.strip())
    def return_data(self):
        return self.dat
>>> with open('sample.html') as htmltext:
        htmldata = htmltext.read()
>>> parser = MyHtmlParser()
>>> parser.feed(htmldata)
>>> res = parser.return_data()
>>> res = [item for item in filter(None, res)]
>>> res[0]
'BBcode'
>>> 

【讨论】:

  • 感谢您的回答!实际上,当我运行此脚本并尝试打印 res[0] 时,我得到了 html 的这一部分: box-shadow { -moz-box-shadow: 3px 3px 5px #000000; -webkit-box-shadow: 3px 3px 5px #000000;盒子阴影:3px 3px 5px #000000; }
  • 哦,没关系,我必须打印第四个参数。正是我需要的。非常感谢!最后一件事,我怎样才能将输出写入文件!?
  • 作为一个简单的文本文件:with open('filename.txt', 'w') as newfile: newfile.write(res[0])
  • 很高兴能提供帮助。我注意到您是 StackOverflow 的新手——欢迎!如果这个或另一个答案解决了您的问题,您可能希望使用左侧的大复选标记接受它作为答案。
【解决方案3】:

我认为您需要添加类似 z.group() 的内容才能退出正则表达式对象,对吗?因此,只需将最后一行更改为

打印 z.group()

可能会这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2013-09-22
    • 2011-08-18
    相关资源
    最近更新 更多