我是一个新手编码(第一个月),我试图创建一个简单的脚本,可以登录到instagram,所以脚本将从CSV文件中获取ID和密码,并在instagram上登录,这是我的代码:
import pandas as pd
import pyperclip
import selenium
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
df = pd.read_csv('/Users/Downloads/scraping2.csv')
print(df)
def instagram_login():
print(df.to_string())
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=uc.Chrome()
driver.get('https://www.instagram.com/')
driver.maximize_window() #schermo intero
consent= driver.find_element(By.XPATH,"/html/body/div[1]/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
time.sleep(5)
put_username = driver.find_element(By.NAME,("username")).send_keys(pyperclip.paste()) #inserisco username
df2=df.at[0,'PASSWORD'] #Trova password
pyperclip.copy(df2) #Copia password
put_password = driver.find_element(By.NAME,("password")).send_keys(pyperclip.paste()) #inserisco password
login = driver.find_element(By.XPATH,"/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/section/main/article/div[2]/div[1]/div[2]/form/div/div[3]/button").click() #clicco login
time.sleep(6)
try:
phone_request = driver.find_element(By.XPATH,"/html/body/div[1]/section/div/div/div[1]/div/p") #clicco su non adesso
if phone_request.is_displayed(): #se è visibile la richiesta telefonica vai avanti
df.drop([2], axis=0,inplace=True) #elimina la prima riga
df
print(df)
except:
pass
try:
wrong_password = driver.find_element(By.ID,"slfErrorAlert")
print(wrong_password.text)
df.drop([2],inplace=True) #elimina la prima riga
print(df)
except:
pass
instagram_login()
这里的问题在于:
wrong_password = driver.find_element(By.ID,"slfErrorAlert")
print(wrong_password.text)
df.drop([2],inplace=True) #elimina la prima riga
print(df)
df.drop似乎不起作用。
我做错了什么?
我需要每次尝试instagram登录页面,在脚本完成操作后,第一行将从CSV中删除
1条答案
按热度按时间xqk2d5yq1#
通常你会尽量避免在DataFrames中循环,特别是对于像你正在调用的这样简单的函数。我个人建议使用
df.apply(my_function, axis=1)
。它会将你的函数应用到每一行。例如:有关详细信息,请通读pandas documentation
除此之外,如果您仍然希望您原来的问题得到回答,我需要更多的信息,为什么您认为
df.drop(...)
不工作。