与PHP在Python中的preg_match相对应 [英] Counterpart to PHP’s preg_match in Python

查看:178
本文介绍了与PHP在Python中的preg_match相对应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划将我的一个抓取器移至Python.我很喜欢在PHP中使用preg_matchpreg_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.

看看这个为什么不应使用正则表达式解析html

这篇关于与PHP在Python中的preg_match相对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆