【问题标题】:FileNotFoundError: [Errno 2] No such file or directory: 'following.txt error accessing a file using Selenium PythonFileNotFoundError: [Errno 2] No such file or directory: 'following.txt 使用 Selenium Python 访问文件时出错
【发布时间】:2021-12-31 07:43:56
【问题描述】:

我有问题。我正在尝试学习如何使用 Selenium 对机器人进行编码。一切正常,但我无法将我从 Instagram 获得的关注者列表保存为 txt 文件。提前感谢您的帮助。

def getFollowing(self):
        self.browser.get(f"https://www.instagram.com/{self.username}")
        time.sleep(3)
        self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[3]/a").click()
        time.sleep(3)

        dialog = self.browser.find_element_by_css_selector("div[role=dialog] ul")
        followingCount = len(dialog.find_elements_by_css_selector("li"))

        print(f"First count: {followingCount}")

        action = webdriver.ActionChains(self.browser)

        while True:
            dialog.click()
            action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()

            newCount = len(dialog.find_elements_by_css_selector("li"))

            if followingCount != newCount:
                followingCount = newCount
                print(f"New count: {followingCount}")
                time.sleep(3)
            else:
                break

            following = dialog.find_elements_by_css_selector("li")

            followingList =[]
            i = 0
            for user in following:
                link = user.find_element_by_css_selector("a").get_attribute("href")
                followingList.append(link)
                i += 1
                if i == followingCount+1:
                    break

            with open("following.txt", "w", encoding="UTF-8") as file:
                for item in followingList:
                    file.write(item + "\n")

elif choice == 2:
        instagram.getFollowing()
        following = open("following.txt", "r")
        for i in following:
            print(i)

输入您的 COISE:2

c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py:72: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[3]/a").click()
c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py:75: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  dialog = self.browser.find_element_by_css_selector("div[role=dialog] ul")
C:\Users\murat\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py:501: UserWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  warnings.warn("find_elements_by_* commands are deprecated. Please use find_elements() instead")
First count: 12
Traceback (most recent call last):
  File "c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py", line 162, in <module>
    following = open("following.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'following.txt'

【问题讨论】:

  • 你的 InstagramBOT 目录中有以下文件吗?

标签: python selenium filenotfounderror


【解决方案1】:

两件事

  1. 你得到了

    DeprecationWarning: find_element_by_* commands are deprecated
    

为了解决这个问题,你应该改变所有的

find_element_by_* 

命令

find_element(By.XPATH, "XPATH")

或 By.CSS_SELECTOR、By.ID 等。取决于您的选择器。

  1. 没有这样的文件

    FileNotFoundError: [Errno 2] No such file or directory: 'following.txt
    

你正在使用这行代码

with open("following.txt", "w", encoding="UTF-8") as file:

你给了

following.txt

因为它不是full file path,Python 会从你运行程序的地方查看当前项目。

可能它不在当前工作目录中。

请确保它存在,如果不在当前工作目录中,则应提供完整的文件路径。

在 Windows 系统上,它会是这样的:

D:/FolderName/following.txt

这只是一个例子。

【讨论】:

    【解决方案2】:

    首先检查文件following.txt是否存在于工作目录中。

        if followingCount != newCount:
            followingCount = newCount
            print(f"New count: {followingCount}")
            time.sleep(3)
        else:
            break  # <-- this line breaks the loop before writing the file.
    

    PS:尝试在另一个新的 python 脚本中读取和写入文件。如果有效,则意味着在您的主代码中,文件位置不同。

    【讨论】:

      【解决方案3】:

      当您使用 时,您的程序中有一个小问题和一个主要问题,如下所示:


      此错误消息...

      FileNotFoundError: [Errno 2] No such file or directory: 'following.txt'
      

      ...表示您的程序无法在当前工作目录中找到文件 following.txt,即在:

      c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT
      

      解决方案

      要解决此错误,您可以采用以下两种方法中的任何一种:

      • 确保将所需的文本文件 following.txt 放在 c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT 子目录中。

      • 也可以传递所需文本文件的绝对路径following.txt,如下:

        with open("c:/absolute_file_location/following.txt", "w", encoding="UTF-8") as file:
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 2012-08-25
        • 1970-01-01
        • 2022-01-24
        相关资源
        最近更新 更多