【问题标题】:Python Loop read the same CSV DataPython Loop 读取相同的 CSV 数据
【发布时间】: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


【解决方案1】:

谷歌局部和全局变量。您正在函数内部更改 df。这不会更改“全局”df。您需要从函数中return您的 df 或首先将其声明为全局变量。

第一个选项:

df = pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv')
def instagram_login():

    df2=df.at[0,'ID'] #Find the first row id
    .....
    #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)
    return df


for line in len(df):
    df = instagram_login()
    time.sleep(5)
    driver.delete_all_cookies()

第二个选项:

df = pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv')
def instagram_login():

    df2=df.at[0,'ID'] #Find the first row id
    .....
    #here is where the first row got deleted and saved on csv
    global df
    df = pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv').drop(0, axis=0)
    df.to_csv(r'/Users/giuseppeleonardi/Downloads/scraping2.csv', index=False)


for line in len(df):
    instagram_login()
    time.sleep(5)
    driver.delete_all_cookies()

【讨论】:

  • 非常感谢,我解决了!我想太多了
【解决方案2】:

您在函数内部定义的 df 不会改变外部 df。 所以可以返回df,保存到外部df。

data_frame= pd.read_csv('/Users/giuseppeleonardi/Downloads/scraping2.csv')
def instagram_login(df):
    ......
    #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)
    return df


#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):
    data_frame = instagram_login(data_frame)
    time.sleep(5)
    driver.delete_all_cookies()

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多