possibility0
possibility0

Reputation: 121

Searching string in a webpage using regular expression on Python?

I want to search if there is /[some names]/unfollow in a webpage. And I have very little experience on regular expressions. This is what I worked for now.

import urllib
import re

page = urllib.urlopen('http://www.domain.com').read()
results = re.findall('/[\w]*/unfollow', page)
for i in results:
    print i

But the code above not printing anything. Am I doing it wrong? If so, I really need help from you guys

Thanks

Upvotes: 0

Views: 2187

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Your findall function should be,

results = re.findall(r'\/[^\/]*\/unfollow', page)

It will findall all the strings which are in /some names/unfollow format.

Explanation:

  • \/ Matches a literal / symbol.
  • [^\/]* Matches any character not of / zero or more times.
  • \/unfollow Matches the string /unfollow

Upvotes: 1

Related Questions