Python 如何连接到页面并检查页面中是否存在单词
我希望每5秒钟通过urllib2连接到一个站点,并获取所有html 在此之后,我想检查一个单词是否在页面中 例如,我想每5秒钟连接一次,检查谷歌是否在页面中 我尝试以下代码:Python 如何连接到页面并检查页面中是否存在单词,python,Python,我希望每5秒钟通过urllib2连接到一个站点,并获取所有html 在此之后,我想检查一个单词是否在页面中 例如,我想每5秒钟连接一次,检查谷歌是否在页面中 我尝试以下代码: import urllib2 import time while true: values = urllib2("http://google.com") if "google" in values: print("google is in my address") else:
import urllib2
import time
while true:
values = urllib2("http://google.com")
if "google" in values:
print("google is in my address")
else:
print("google not in my address")
time.sleep(5)
我使用的是python 2.7,您的代码有错误
将代码更改为:
import urllib2
import time
while true:
values = urllib2.urlopen("http://google.com").read()
if "google" in values:
print("google is in my address")
else:
print("google not in my address")
time.sleep(5)
这段代码每5秒钟检查一次你的站点你有没有试过阅读?作为旁注,你可能想使用更高级别的抽象,比如优秀的包是的,我读过urllib,但不理解它