Else是無效語法

我寫了一些代碼,將下載一個網頁,然后搜索某個字符串,我知道這是一個低效的方式來做它,但這是我選擇的方式,無論如何,代碼總是出現一個無效的語法為其他人。歡迎任何幫助!代碼:

import requests
from discord import Webhook, RequestsWebhookAdapter

#download website and turns into a .txt file
while true:
    print('Beginning file download with requests')
    url = str(input("Enter Profile ID:"))
    r = requests.get(url)
    with open('/Users/Computer/Desktop/Notification/[profile.txt', 'wb') as f:
     f.write(r.content)

# Retrieve HTTP meta-data
print(r.status_code)
print(r.headers['content-type'])
print(r.encoding)
#opens text file and searches for a certain keyword
with open('/Users/Computer/Desktop/Notification/[profile.txt') as f:
    if 'avatar-status online profile-avatar-status icon-online' in f.read():#if it finds the keyword it sleeps and then retries
        time.sleep(20)
    continue
#if it doesnt find the keyword (meaning they are offline) it sends you a message through discord webhook
    else:
    webhook = Webhook.from_url("YOUR WEBHOOK HERE!", adapter=RequestsWebhookAdapter())
    webhook.send("Found string")
? 最佳回答:
  • 你需要在True中大寫T
  • 您的continuewhile之外,因為它沒有縮進。
  • 您的continueif之外,因為它沒有縮進,這就斷開了elseif的連接
  • 你的else正文沒有縮進
while True: # Capitalize
    print('Beginning file download with requests')
    url = str(input("Enter Profile ID:"))
    r = requests.get(url)
    with open('/Users/Computer/Desktop/Notification/[profile.txt', 'wb') as f:
        f.write(r.content)

    # Indent everything below this
    print(r.status_code)
    print(r.headers['content-type'])
    print(r.encoding)
    #opens text file and searches for a certain keyword
    with open('/Users/Computer/Desktop/Notification/[profile.txt') as f:
        if 'avatar-status online profile-avatar-status icon-online' in f.read():
            time.sleep(20)
            continue # indent this to align with the sleep
        else:
            webhook = Webhook.from_url("YOUR WEBHOOK HERE!", adapter=RequestsWebhookAdapter()) # indent this to be in the else
            webhook.send("Found string") # indent this too

進一步細化:

  • 你根本不需要那個額外的文件,搜索r.content一個字節字符串
  • input()返回str(),所以不需要轉換它
  • if/else之后什么都沒有,所以你不需要continue
while True:
    print('Beginning file download with requests')
    url = input("Enter Profile ID:")
    r = requests.get(url)
    print(r.status_code)
    print(r.headers['content-type'])
    print(r.encoding)
    if b'avatar-status online profile-avatar-status icon-online' in r.content:
        time.sleep(20)
    else:
        webhook = Webhook.from_url("YOUR WEBHOOK HERE!", adapter=RequestsWebhookAdapter())
        webhook.send("Found string")
主站蜘蛛池模板: 一区二区三区免费在线观看| 久久精品国内一区二区三区| 一区二区三区在线播放| 日韩精品一区二区三区中文字幕| 一区二区三区四区视频在线| 免费av一区二区三区| 无码人妻精品一区二区三区久久久| 无码人妻一区二区三区在线水卜樱 | jizz免费一区二区三区| 国产成人一区二区三区视频免费| 亚洲美女一区二区三区| 国产精品高清一区二区三区| 亚洲AV无码一区二三区| 国产精品亚洲一区二区三区| 亚洲AV无码国产精品永久一区| 亚洲国产一区二区视频网站| 成人精品一区二区三区电影| 亚洲AV香蕉一区区二区三区| 日本一区二区三区爆乳| 亚洲一区二区三区国产精华液| 无码人妻久久一区二区三区| 国产成人无码精品一区二区三区| 国产成人精品一区二区三区免费| 国产成人精品无码一区二区三区| 国产在线精品一区二区三区直播 | 精品乱码一区二区三区四区| 国产人妖视频一区二区| 国产另类TS人妖一区二区| 国产激情一区二区三区 | 中文字幕在线一区二区三区| 精品日韩在线视频一区二区三区| 日本中文一区二区三区亚洲| 日本精品一区二区三区在线视频一 | 91精品一区国产高清在线| 久久国产精品免费一区二区三区| 亚洲av成人一区二区三区在线观看 | 精品免费国产一区二区三区| 男人的天堂亚洲一区二区三区| 黑人大战亚洲人精品一区| 精品日韩亚洲AV无码一区二区三区| 性色AV一区二区三区天美传媒|