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

2019-07-27 07:06发布

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

标签: python http
3条回答
Rolldiameter
2楼-- · 2019-07-27 07:29

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)
查看更多
虎瘦雄心在
3楼-- · 2019-07-27 07:30

you can use urllib2

import urllib2

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

webp.find("the_word")

hope that helps :D

查看更多
Ridiculous、
4楼-- · 2019-07-27 07:48

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.

查看更多
登录 后发表回答