2016-09-14 37 views
-3

存在,我想通过urllib2每5 2连接到一个网站,并获得所有的HTML 在这之后,我想查一个词在页面或不如何连接到网页,并检查一个单词在

例如我想连接到google每5二,检查谷歌在网页或不

我试试这个代码:

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

+0

你甚至尝试阅读[urllib的文档(https://开头的文档。 python.org/2/library/urllib2.html)? – spectras

+0

(作为旁注,您可能想要使用更高层次的抽象,比如优秀的[请求](http://docs.python-requests.org/en/master/)包) – spectras

+0

是的,我读了'urllib'但是不理解它 – nonename

回答

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第二次检查您的网站