运行 在 python 中每 x 秒循环一次以抓取网站

Run a loop every x seconds in python in order to scrape a website

我正在抓取一个网站,当我 运行 我的脚本时,它使 API 请求崩溃。我想在每次迭代之间中断 3 秒来迭代循环。

def printit():
    country_list_id = ['a', 'b', 'c']
    product_list_id = ['1', '2', '3']

    for c in country_list_id:
       for p in product_list_id:
          url = "https://www.example.com/api/country_id=" + c + "&product_id=" + p + "&option="
        test(url)

def test(url_final):
        get_url = requests.get(url_final)
        get_text = get_url.text
        print(get_text)
        #I'd like to make a small break here before it iterate again


printit()

首先,导入time:

import time

然后 sleep 对于 3:

time.sleep(3)

使用time模块:

import time

     def printit():
        country_list_id = ['a', 'b', 'c']
        product_list_id = ['1', '2', '3']

        for c in country_list_id:
           for p in product_list_id:
              url = "https://www.example.com/api/country_id=" + c + "&product_id=" + p + "&option="
            test(url)

    def test(url_final):
            get_url = requests.get(url_final)
            get_text = get_url.text
            print(get_text)
            time.sleep(3)


    printit()