通过网页搜索 [英] Searching through webpage

查看:72
本文介绍了通过网页搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在研究一个Python项目,该项目需要我浏览一个网页.我想浏览以查找特定的文本,如果找到了该文本,则将其打印出来.如果不是,它将打印出错误消息.我已经尝试过使用其他模块(例如libxml),但是我不知道该怎么做.

Hey I'm working on a Python project that requires I look through a webpage. I want to look through to find a specific text and if it finds the text, then it prints something out. If not, it prints out an error message. I've already tried with different modules such as libxml but I can't figure out how I would do it.

有人可以帮忙吗?

推荐答案

您可以执行以下简单操作:

You could do something simple like:


import urllib2
import re

html_content = urllib2.urlopen('http://www.domain.com').read()

matches = re.findall('regex of string to find', html_content);

if len(matches) == 0: 
   print 'I did not find anything'
else:
   print 'My string is in the html'

这篇关于通过网页搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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