16

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?

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);

Whereas in Python I am not able to figure out a similar function.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
funnyguy
  • 513
  • 3
  • 6
  • 15
  • 1
    Here's the python regex docs: http://docs.python.org/howto/regex.html – Ben Lee Jan 30 '12 at 09:39
  • 2
    In Python we don't use regular expressions for parsing HTML, we use [BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/). See http://stackoverflow.com/a/1732454/78845 – johnsyweb Jan 30 '12 at 09:44

3 Answers3

14

You looking for python's re module.

Take a look at re.findall and re.search.

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.

Take a look at this Why you should not parse html with regex

Community
  • 1
  • 1
RanRag
  • 48,359
  • 38
  • 114
  • 167
  • Thanks gentlemen for your replies. I have started using Beatifulsoup and I am facing some problems using it. I have passed the html data to Beatifulsopu and I am facing this error. soup = BeautifulSoup(data) print soup.prettify() line 52, in soup = BeautifulSoup(data) File "/home/infoken-user/Desktop/lin/BeautifulSoup.py", line 1519, in __init__ BeautifulStoneSoup.__init__(self, *args, **kwargs) File "/home/infoken-user/Desktop/lin/BeautifulSoup.py", line 1144, .. '^<\?.*encoding=[\'"](.*?)[\'"].*\?>').match(xml_data) TypeError: expected string or buffer – funnyguy Jan 30 '12 at 12:54
5

I think you need somthing like that:

output = re.search('a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE)
    if output is not None:
        output = output.group(0)
        print(output)

you can add (?s) at the start of regex to enable multiline mode:

output = re.search('(?s)a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE)
    if output is not None:
        output = output.group(0)
        print(output)
Vasin Yuriy
  • 484
  • 5
  • 13
2

You might be interested in reading about Python Regular Expression Operations

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72