2024-04-19 13:51:30 发布
网友
我打算把我的一个刮刀移到Python上。我很乐意在PHP中使用preg_match和preg_match_all。我在Python中找不到类似于preg_match的合适函数。有人能帮我吗?
preg_match
preg_match_all
例如,如果我想获取<a class="title"和</a>之间的内容,我在PHP中使用以下函数:
<a class="title"
</a>
preg_match_all('/a class="title"(.*?)<\/a>/si',$input,$output);
而在Python中,我无法找到类似的函数。
你在找Python的re module。
看看re.findall和re.search。
正如您所提到的,您正在尝试解析html,为此使用html parsers。python中有两个选项可用,比如lxml或BeautifulSoup。
html parsers
看看这个Why you should not parse html with regex
我想你需要这样的东西:
output = re.search('a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE) if output is not None: output = output.group(0) print(output)
你可以加上?s) 在regex开始时启用多行模式:
output = re.search('(?s)a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE) if output is not None: output = output.group(0) print(output)
你可能对阅读Python Regular Expression Operations感兴趣
你在找Python的re module。
看看re.findall和re.search。
正如您所提到的,您正在尝试解析html,为此使用
html parsers
。python中有两个选项可用,比如lxml或BeautifulSoup。看看这个Why you should not parse html with regex
我想你需要这样的东西:
你可以加上?s) 在regex开始时启用多行模式:
你可能对阅读Python Regular Expression Operations感兴趣
相关问题 更多 >
编程相关推荐