Skip to main content
 首页 » 编程设计

需要 Python 正则表达式帮助

2025年05月04日8bluestorm

我需要从在 <font color="red">needed-info-here</font> 之间输出信息的网站获取信息或者 <span style="font-weight:bold;">needed-info-here</span> , 随机。

用了就可以了

start = '<font color="red">' 
end = '</font>' 
expression = start + '(.*?)' + end 
match = re.compile(expression).search(web_source_code) 
needed_info = match.group(1) 

,但我必须选择获取 <font>或 <span> ,失败,当网站使用其他标签时。

如何修改正则表达式使其始终成功?

请您参考如下方法:

Don't parse HTML with regex.

Regex 不是解决此问题的正确工具。查找BeautifulSoup或 lxml .