How to open a webpage and search for a word in python?
Asked
Active
Viewed 2.9k times
3 Answers
4
This is a little simplified:
>>> import urllib
>>> import re
>>> page = urllib.urlopen("http://google.com").read()
# => via regular expression
>>> re.findall("Shopping", page)
['Shopping']
# => via string.find, returns the position ...
>>> page.find("Shopping")
2716
First, get the page (e.g. via urllib.urlopen
). Second use a regular expression to find portions of the text, you are interested in. Or use string.find
.

miku
- 181,842
- 47
- 306
- 310
-
1Not much point using `re.compile` if you're not saving the compiled regexp to a variable. `re.findall("Shopping", page)` is simpler. – Daniel Roseman Dec 16 '09 at 13:30
0
you can use urllib2
import urllib2
webp=urllib2.urlopen("the_page").read()
webp.find("the_word")
hope that helps :D

Ahmad Dwaik
- 963
- 1
- 9
- 13
0
How to open a webpage?
I think the most convinient way is:
from urllib2 import urlopen
page = urlopen('http://www.example.com').read()
How to search for a word?
I guess you are going to search for some pattern in the page next, so here we go:
import re
pattern = re.compile('^some regex$')
match = pattern.search(page)

satoru
- 31,822
- 31
- 91
- 141