如何在20次硒刮除请求后等待30秒

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

你好,我有一个csv文件300个数据。

10个请求后,网站停止为我提供结果。

如何在10个请求后暂停我的脚本3分钟

谢谢

我的代码:

societelist =[]


import csv



with open('1.csv') as csvfile:
  reader = csv.reader(csvfile) 
  for row in reader:
    browser = webdriver.Firefox(options=options)
    browser.get("myurl".format(row[0]))
    time.sleep(20) 

    try:
        societe = browser.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[2]/div[1]/span[2]').text
    except NoSuchElementException:
        societe = 'Element not found'

    societelist.append(societe)
    print (row[0])
    browser.quit()

df = pd.DataFrame(list(zip(societelist)), columns=['societe'])

data = df.to_csv('X7878.csv', index=False)
selenium export-to-csv scrape pause
2个回答
0
投票

用途:

import csv
societelist =[]

with open('1.csv') as csvfile:
    reader = csv.reader(csvfile) 
    for i, row in enumerate(reader):       # i gives the index of the row.
        browser = webdriver.Firefox(options=options)
        browser.get("myurl".format(row[0]))
        time.sleep(20) 

        try:
            societe = browser.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[2]/div[1]/span[2]').text
        except NoSuchElementException:
            societe = 'Element not found'

        societelist.append(societe)
        print(row[0])
        browser.quit()
        if not ((i+1) % 10):    
            time.sleep(180)

df = pd.DataFrame(list(zip(societelist)), columns=['societe'])
df.to_csv('X7878.csv', index=False)

0
投票

感谢季玮,我该如何在excel文件中而不是在抓取结束时动态添加要抓取的结果?

thks

© www.soinside.com 2019 - 2024. All rights reserved.