我寫了一些代碼,將下載一個網頁,然后搜索某個字符串,我知道這是一個低效的方式來做它,但這是我選擇的方式,無論如何,代碼總是出現一個無效的語法為其他人。歡迎任何幫助!代碼:
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
continue
在while
之外,因為它沒有縮進。continue
在if
之外,因為它沒有縮進,這就斷開了else
與if
的連接else
正文沒有縮進進一步細化:
r.content
一個字節字符串input()
返回str()
,所以不需要轉換它if
/else
之后什么都沒有,所以你不需要continue