How to open a webpage and search for a word in pyt

2019-07-27 06:50发布

问题:

How to open a webpage and search for a word in python?

回答1:

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.



回答2:

you can use urllib2

import urllib2

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

webp.find("the_word")

hope that helps :D



回答3:

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)


标签: python http