How to open a webpage and search for a word in python?
+1
A:
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
.
The MYYN
2009-12-16 10:59:00
Not much point using `re.compile` if you're not saving the compiled regexp to a variable. `re.findall("Shopping", page)` is simpler.
Daniel Roseman
2009-12-16 13:30:24
A:
you can use urllib2
import urllib2
webp=urllib2.urlopen("the_page").read()
webp.find("the_word")
hope that helps :D
Ahmad Dwaik
2009-12-16 11:02:18
A:
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.Logic
2009-12-16 13:34:06