如何连接到页面并检查页面中是否存在单词

时间:2016-09-14 07:30:43

标签: python

我希望每5秒钟通过urllib2连接一个网站并获取所有HTML 在此之后我想检查一个单词是否在页面中

例如我希望每5秒钟连接到google并检查google是否在页面中

我试试这段代码:

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

1 个答案:

答案 0 :(得分:1)

您的代码有错误

将您的代码更改为:

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秒检查一次您的网站