循环并检查其刷新时间是否正在为此解决方案?

问题描述 投票:0回答:1

我有一个循环,大约每10个小时左右一次,反复刮擦30个链接,所以我更喜欢清除其他列表,其中保留了某些找到的数据。这是一个抽象的例子。如果我在单独的线程中有时间刷新器。

def check_refresh(time_since_refresh):
        time_difference = round((datime.now() - time_since_refresh).total_seconds()/60/60)
        if time_difference == CLEAR_FOUND:
            time_since_refresh = datetime.now()
            return True
        return False


while True:
  scrape(url)
  if check_refresh():
     temporary_list.clear()

事实是,抓取的速度对我来说很重要,因此,如果经历每个循环,检查是否该刷新,我觉得它会降低速度。刷新的响亮时间应该是一个单独的线程,并且具有一个布尔值标志,抓取器将通过每个循环读取该标志?还有没有一种更好的方法可以实现自循环开始以来没有check_refresh的“ hack”就花费了多少时间?

python multithreading web-scraping
1个回答
0
投票

是的,线程化是一个更好的主意。试试这个:

def check_refresh(time_since_refresh):
        time_difference = round((datime.now() - 
                time_since_refresh).total_seconds()/60/60)
        if time_difference == CLEAR_FOUND:
            time_since_refresh = datetime.now()
            return True
        return False

t = threading.Thread(target=check_refresh)
while True:
  scrape(url)
  if t:
     temporary_list.clear()
© www.soinside.com 2019 - 2024. All rights reserved.