与PHP在Python中的preg_match相对应 [英] Counterpart to PHP’s preg_match in Python
问题描述
我正计划将我的一个抓取器移至Python.我很喜欢在PHP中使用preg_match
和preg_match_all
.我在Python中找不到与preg_match
类似的合适函数.有人可以帮我吗?
I am planning to move one of my scrapers to Python. I am comfortable using preg_match
and preg_match_all
in PHP. I am not finding a suitable function in Python similar to preg_match
. Could anyone please help me in doing so?
例如,如果要获取<a class="title"
和</a>
之间的内容,请在PHP中使用以下功能:
For example, if I want to get the content between <a class="title"
and </a>
, I use the following function in PHP:
preg_match_all('/a class="title"(.*?)<\/a>/si',$input,$output);
在Python中,我无法找出类似的功能.
Whereas in Python I am not able to figure out a similar function.
推荐答案
您在寻找python的 re模块.
You looking for python's re module.
看看 re.findall 和 re.search .
并且正如您所提到的,您正在尝试解析html,请使用html parsers
. python中有几个选项,例如 lxml 或
And as you have mentioned you are trying to parse html use html parsers
for that. There are a couple of option available in python like lxml or BeautifulSoup.
这篇关于与PHP在Python中的preg_match相对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!