Skip to main content
 首页 » 编程设计

Python正则表达式

2025年05月04日25yxwkf

str1 = abdk3<h1>The content we need</h1>aaaaabbb<h2>The content we need2</h2> 

我们需要h1标签和h2标签里面的内容。

最好的方法是什么?谢谢

感谢您的帮助!

请您参考如下方法:

如果需要扩展,最好的方法是使用 BeautifulSoup 之类的东西。

>>> from BeautifulSoup import BeautifulSoup 
>>> soup = BeautifulSoup('abdk3<h1>The content we need</h1>aaaaabbb<h2>The content we need2</h2>') 
>>> soup.h1 
<h1>The content we need</h1> 
>>> soup.h1.text 
u'The content we need' 
>>> soup.h2 
<h2>The content we need2</h2> 
>>> soup.h2.text 
u'The content we need2' 

它也可以用正则表达式来完成,但这可能更符合你的需要。你想要的一个更大的例子可能很好。如果不完全了解您想要解析的内容,就很难提供适当的帮助。