用python在html上查找单词

2024-09-30 04:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个python3脚本,在网页上寻找像“ArduinoON”之类的词。但进展并不顺利。我知道如何使用php,但不知道如何使用python。下面是我如何用php做的,以及我如何用python做的。你知道吗

下面是我如何用php实现的。你知道吗

    <?php
$data = file_get_contents('http://192.168.0.200:9090');
$regex = '/Switch1On/';

if (preg_match($regex, $data)) {
   echo '<font color="green">on</font>';
} else {
   echo '<font color="red">off</font>';
}
?>

我的python代码

 import urllib.request

x = urllib.request.urlopen('http://192.168.0.200:9090/')
if "ArduinoOn" in x:
     print ("True")

有人能帮我吗?你知道吗


Tags: echo脚本http网页dataifrequesturllib
2条回答

如果要在html页面中查找文本字符串,可以使用Pythonrequests模块。这可能是最简单的方法。你知道吗

import requests

r = requests.get('http://192.168.0.200:9090/')
if "ArduinoOn" in r.text:
    print ('True')

docs之后,urllib.request.urlopen返回一个http.client.HTTPResponse对象。因此,如果您想获取页面内容,您应该阅读:

x = urllib.request.urlopen('http://192.168.0.200:9090/')
if 'Arduino On' in x.read():
    print 'True'

希望这有帮助。你知道吗

相关问题 更多 >

    热门问题