tags:

views:

117

answers:

3

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
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
A: 

you can use urllib2

import urllib2

webp=urllib2.urlopen("the_page").read()

webp.find("the_word")

hope that helps :D

Ahmad Dwaik
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
how to copy an answer?
SilentGhost
@SilentGhost what do you mean by copying an answer?
Satoru.Logic