如何在python中打开网页并搜索单词

时间:2009-12-16 10:53:13

标签: python http

如何打开网页并在python中搜索单词?

3 个答案:

答案 0 :(得分:3)

这有点简化:

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

首先,获取页面(例如通过urllib.urlopen)。第二次使用regular expression查找您感兴趣的部分文本。或者使用string.find

答案 1 :(得分:0)

你可以使用urllib2

import urllib2

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

webp.find("the_word")

希望有所帮助:D

答案 2 :(得分:0)

如何打开网页?

我认为最方便的方式是:

from urllib2 import urlopen

page = urlopen('http://www.example.com').read()

如何搜索单词?

我猜你接下来会在页面中搜索一些模式,所以我们走了:

import re
pattern = re.compile('^some regex$')
match = pattern.search(page)