我有一段代码用于实时子计数,效果很好,但是我想将这些结果添加到名为“ history.txt”的文件中,但是当我运行代码时,即使我将其放置了相当长的时间,为了使循环循环很多次,我的txt文件保持为空。
(下面的代码)(我知道它草率的不要判断我)
import requests
import time
import os
from time import gmtime, strftime
open("history.txt", "w").close()
histTx = open("history.txt", "a")
def simp():
global histTx
u = input("Input the URL of the channel page:\n")
p = True
while p:
try:
u.index("http")
except ValueError:
u = "http://" + u
response = requests.get(u)
r = str(response.content).lower()
locator = r.index("subscribers")
goBack = locator
while not r[goBack] == "\"":
goBack -= 1
goBack += 1
os.system('clear')
print("\033[1;36;48m", r[goBack : locator].upper())
print("---------\n")
time.sleep(2.5)
os.system('clear')
histTx.write(str(r[goBack : locator].upper()))
def adv():
global histTx
u = input("Input the URL of the channel page (input -1 for simple mode):\n")
if u == "-1":
simp()
else:
p = True
while p:
try:
u.index("http")
except ValueError:
u = "http://" + u
response = requests.get(u)
r = str(response.content).lower()
locator = r.index("subscribers")
goBack = locator
while not r[goBack] == "\"":
goBack -= 1
goBack += 1
os.system('clear')
print("That channel has \033[1;36;48m" + r[goBack : locator].upper() + "\033[0mSubscribers.\n")
print("---------\n")
time.sleep(2.5)
os.system('clear')
histTx.write(str(r[goBack : locator].upper()))
adv()
(在您询问之前,没有错误,并且history.txt确实存在。)