如何打开网页并在python中搜索单词 [英] How to open a webpage and search for a word in python

查看:60
本文介绍了如何打开网页并在python中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这有点简化:

 >>>导入urllib>>>汇入>>>页面= urllib.urlopen("http://google.com").read()#=>通过正则表达式>>>re.findall(购物",第页)['购物']#=>通过string.find返回位置...>>>page.find(购物")2716 

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

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

解决方案

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.

这篇关于如何打开网页并在python中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆