Как ждать 30 секунд после 20 запросов очистки селен - PullRequest
0 голосов
/ 20 марта 2020

Здравствуйте, у меня есть CSV-файл на 300 данных.

После 10 запросов веб-сайт перестает давать мне результаты.

Как сделать паузу на 3 минуты моего скрипта после 10 запросов

спасибо

мой код:

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)

1 Ответ

0 голосов
/ 20 марта 2020

Использование:

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)

Альтернативное решение для записи каждой строки текста в Excel после очистки вместо записи всего текста сразу.

import csv
import win32com.client as win32

# Launch excel
excel = win32.Dispatch('Excel.Application')
excel.Visible = 1
wb = excel.Workbooks.Add()
ws = wb.Sheets(1)

# Read csv and scrape webpage
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'

        # it may make sense to write the input text and the scraped value side by side.
        ws.Cells(i+1, 1).Value = row[0]
        ws.Cells(i+1, 2).Value = societe

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

# If you want to save the file programmatically and close excel.
path = r'C:\Users\jarodfrance\Documents\X7878.xlsx'
wb.SaveAs(path)
wb.Close()
excel.Quit()
...