我正在尝试在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>
我想从[到]中提取所有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对象……错误在哪里?
我将为此使用解析器:
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'
>>>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句