用于检查页面的脚本

时间:2016-08-26 18:13:47

标签: python

我有一个脚本,当网页上的短语发生变化时,会通过邮件提醒我。我尝试了很多东西,但是我无法修复 isAvailable()功能:每次脚本都说“不可用”,无论我是否给它一个可用的服务器。你有任何线索吗?

# CONFIG
TARGET_KIMSUFI_ID = "160sk1" # something like 160sk1
TARGET_DESCR      = ""
EMAIL_FROM_ADDRS  = ""
EMAIL_TO_ADRS     = ""
EMAIL_SMTP_LOGIN  = EMAIL_FROM_ADDRS
EMAIL_SMTP_PASSWD = ""
EMAIL_SMTP_SERVER = ""

# CODE
import urllib.request
import smtplib
import time

def isAvailable():
    rawPageContent = urllib.request.urlopen("https://www.kimsufi.com/en/servers.xml").read()
    rawPageContent = str(rawPageContent)
    poz = rawPageContent.find(TARGET_KIMSUFI_ID)
    row = rawPageContent[poz:]
    poz = row.find("</tr>")
    row = row[:poz]
    searchText = "Currently being replenished"
    poz = row.find(searchText)
    return poz != -1

def sendEmailWithMessageAvailable():
    msg = "From: KIMSUFI HUNTER <"+EMAIL_FROM_ADDRS+">\r\n"+\
        "To: "+EMAIL_TO_ADRS+"\r\n"+\
        "Subject: [KIMSUFI] "+TARGET_DESCR+" is now AVAILABLE!\r\n"+\
        "\r\n"+\
        "kimsufi-hunter.py has detected that "+TARGET_DESCR+" is now ["+time.ctime()+"] available!\r\n"+\
        "https://www.kimsufi.com/en/\r\n"
    server = smtplib.SMTP(EMAIL_SMTP_SERVER)
    server.starttls()
    server.login(EMAIL_SMTP_LOGIN,EMAIL_SMTP_PASSWD)
    server.sendmail(EMAIL_FROM_ADDRS, EMAIL_TO_ADRS, msg)
    server.quit()

while True:
    if isAvailable():
        print(time.ctime() + " -- KIMSUFI "+TARGET_DESCR+" not available")
        nextSleep = 5 #5secs
    else:
        print(time.ctime() + " -- KIMSUFI "+TARGET_DESCR+" AVAILABLE!!! -- sleeping for 5 minutes")
        sendEmailWithMessageAvailable()
        nextSleep = 5*60 #5mins
    time.sleep(nextSleep)

1 个答案:

答案 0 :(得分:1)

如果网站可用,您的isAvailable()函数会返回True,否则会返回False。您应该将if语句更改为:

if not isAvailable():
    ...