用python在html上查找单词


look for words on html with python

im试图制作一个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")

有人能帮我吗?

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

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

在文档之后,urllib.request.urlopen返回一个http.client.HTTPResponse对象。因此,如果你想获得页面的内容,你应该阅读它:

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

希望这能有所帮助。