Pandas df.drop()失效:Instagram登录脚本无法删除CSV首行
问题描述
我是编程新手(入门第一个月),正在编写一个Instagram登录脚本,从CSV文件读取账号和密码进行登录。但脚本中的df.drop()操作无法删除CSV的首行,需求是每次登录尝试完成后删除CSV的首行,目前该操作未生效,请问问题出在哪里?
代码如下:
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()似乎无法正常工作。
问题分析与解决
你的df.drop()操作失效主要有两个核心原因,对应修改方案如下:
1. 错误指定了要删除的行索引
你写的df.drop([2], inplace=True)是删除索引为2的行,而不是首行。默认情况下,Pandas读取CSV后首行的索引是0,所以要删除首行应该改成:
df.drop([0], axis=0, inplace=True)
另外,你在phone_request分支里的df.drop([2], ...)也是同样的错误,需要一起修正。
2. 只修改了内存中的DataFrame,未写回CSV文件
就算你正确删除了DataFrame里的行,这个修改只存在于内存中,并没有保存到原CSV文件里。所以下次运行脚本时,还是会读取到原来的内容。删除行后,必须添加代码把修改后的DataFrame写回CSV:
df.to_csv('/Users/Downloads/scraping2.csv', index=False)
index=False是为了避免把Pandas的索引列写入CSV,导致文件格式混乱。
修正后的代码片段示例
以wrong_password分支为例,修正后应该是:
try: wrong_password = driver.find_element(By.ID,"slfErrorAlert") print(wrong_password.text) df.drop([0], inplace=True) # 删除首行 df.to_csv('/Users/Downloads/scraping2.csv', index=False) # 写回CSV print(df) except: pass
phone_request分支也需要做同样的修改。
额外提示
- 尽量避免使用全局变量
df在函数内直接修改,你可以把df作为参数传入函数,或者在函数内返回修改后的df再处理,这样代码更规范。 - 你的代码里有很多硬编码的XPATH,Instagram页面结构变化后这些路径会失效,建议使用更稳定的定位方式,比如结合
By.CSS_SELECTOR或者更短的XPATH。
内容的提问来源于stack exchange,提问作者sdogo
相关产品推荐
相关产品推荐

