随着时间的流逝,While 循环变慢 Python

While loop slows down as time passes on Python

我正在尝试从一个检测现场足球赔率下降的网站上抓取一些数据,如果页面的 HTML 有特定的变化,它会向我发送一个电报机器人通知我做的……这是我的代码:

from distutils.command.clean import clean
import time
import requests
from bs4 import BeautifulSoup as bs
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

ids_list=[]
game_urls=[] 
game_name=[]
gfix=[]
livecapper_url ="https://livecapper.ru/bet365/" #the website link

while(True):
    page=requests.get(livecapper_url,verify=False).text
    soup = bs(page , "html.parser")
    game_ids = soup.find_all(game_id=True) #getting the IDs of every football game
    for g in game_ids:
            x=g.get('game_id')
            ids_list.append(x)   #putting the IDs on a list

    for id in ids_list:
            game_url = f"https://livecapper.ru/bet365/event.php?id={id}" #the URL of every single football game
            game_urls.append(game_url)

    for g in game_urls:
            response=requests.get(g).text
            soup = bs(response, "html.parser")
            for t in soup.find_all("td",class_=['red1','red2','red3'], limit=1): #detecting the change in HTML
                for g in soup.find_all("h1"):
                    game_name.append(g.get_text()) if g.get_text() not in game_name else game_name

    for f in game_name:
            game_url= 'https://api.telegram.org/botTOKEN/sendMessage?chat_id=-609XXXXXX&text=Fixed Alert : {}'.format(f) #sending notification to telegram bot
            if game_url not in gfix:
                gfix.append(game_url)
                requests.get(game_url)
            else:
                pass       

    ids_list.clear
    game_name.clear
    game_urls.clear
    time.sleep(1)

如您所见,我正在使用 While (True): 方法来 运行 代码 24/7,但问题是每次迭代的持续时间大约是前一次迭代的两倍。

例如 第一次迭代=10s |第二次迭代=20s |第三次迭代=40s |第 4 次迭代=80s

我该怎么做才能使所有迭代尽可能快地工作?

更改这些:

    ids_list.clear
    game_name.clear
    game_urls.clear

至:

    ids_list.clear()
    game_name.clear()
    game_urls.clear()

如果没有括号,您就不会调用这些方法,而只是访问它们然后丢弃它们(即,它什么都不做)。

代码有很多问题,但最终每次花费更长时间的原因是您继续追加到您的列表,因此每次迭代后该列表将变得越来越大(包括重复项)。您可以做几件事:

  1. 将那些初始的空列表放入循环中
  2. 从列表中删除重复项,这样它就不会在每次迭代中多次请求相同的东西
  3. 正确使用.clear()

我只是做了 1,因为看起来你想要的是用一个清晰​​的列表开始每次迭代。

from distutils.command.clean import clean
import time
import requests
from bs4 import BeautifulSoup as bs
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


gfix=[]
livecapper_url ="https://livecapper.ru/bet365/" #the website link

while(True):
    ids_list=[]
    game_urls=[] 
    game_name=[]
    page=requests.get(livecapper_url,verify=False).text
    soup = bs(page , "html.parser")
    game_ids = soup.find_all(game_id=True) #getting the IDs of every football game
    for g in game_ids:
            x=g.get('game_id')
            ids_list.append(x)   #putting the IDs on a list

    for id in ids_list:
            game_url = f"https://livecapper.ru/bet365/event.php?id={id}" #the URL of every single football game
            game_urls.append(game_url)

    for g in game_urls:
            response=requests.get(g).text
            soup = bs(response, "html.parser")
            for t in soup.find_all("td",class_=['red1','red2','red3'], limit=1): #detecting the change in HTML
                for g in soup.find_all("h1"):
                    game_name.append(g.get_text()) if g.get_text() not in game_name else game_name

    for f in game_name:
            game_url= 'https://api.telegram.org/botTOKEN/sendMessage?chat_id=-609XXXXXX&text=Fixed Alert : {}'.format(f) #sending notification to telegram bot
            if game_url not in gfix:
                gfix.append(game_url)
                requests.get(game_url)
            else:
                pass       

    time.sleep(1)