【发布时间】:2022-12-18 11:18:57
【问题描述】:
我必须获取 csv 文件的第一行,并在处理完后将其删除,然后再恢复该行。
我正在尝试构建一个登录系统,从 csv 文件中获取帐户,然后一个一个地登录。
问题是每次启动循环时,它总是采用相同的帐户,我该如何解决?
import pandas as pd
import pyperclip
import selenium
import random
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
import names
df = pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv')
def instagram_login():
df2=df.at[0,'ID'] #Find the first row id
pyperclip.copy(df2) #Copy the first row id to the clipboard
print(pyperclip.paste()) #Print the first row id
#apro il sito
driver.get('https://www.instagram.com/')
driver.maximize_window() #schermo intero
time.sleep(2)
try:
consent= driver.find_element(By.XPATH,"/html/body/div[2]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/button[2]").click() #clicco il consenso
except:
pass
time.sleep(5)
put_username = driver.find_element(By.NAME,("username")).send_keys(pyperclip.paste()) #inserisco username
df2=df.at[0,'PASSWORD'] #find the password
pyperclip.copy(df2) #copy the password
put_password = driver.find_element(By.NAME,("password")).send_keys(pyperclip.paste()) #inserisco password
time.sleep(2)
login = driver.find_element(By.XPATH,"//div[contains(text(),'Accedi')]").click() #Click login
time.sleep(6)
#here is where the first row got deleted and saved on csv
df= pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv').drop(0, axis=0)
df.to_csv(r'/Users/giuseppeleonardi/Downloads/scraping2.csv', index=False)
#this is the loop that always takes the same line of the file every time even though this is canceled at the end of the operation:
for line in len(df):
instagram_login()
time.sleep(5)
driver.delete_all_cookies()
我在谷歌上搜索了很多但无法弄清楚,我读过文件句柄将读取文件一次,我需要循环来每次重置列表并取第一个值,我该怎么做?
抱歉,我还在学习
【问题讨论】:
-
@Ari 抱歉,错过了那部分我已经编辑过了
标签: python-3.x pandas selenium