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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1913871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:20:22  来源:igfitidea点击:

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

pythonhttp

提问by Markus

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

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

回答by miku

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 expressionto find portions of the text, you are interested in. Or use string.find.

首先,获取页面(例如通过urllib.urlopen)。其次使用正则表达式查找您感兴趣的文本部分。或者使用string.find.

回答by Ahmad Dwaik

you can use urllib2

你可以使用 urllib2

import urllib2

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

webp.find("the_word")

hope that helps :D

希望有帮助:D

回答by satoru

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)