发布
社区首页 >问答首页 >FileNotFoundError:[Errno 2]没有这样的文件或目录:'following.txt使用Selenium Python访问文件时出错

FileNotFoundError:[Errno 2]没有这样的文件或目录:'following.txt使用Selenium Python访问文件时出错
EN

Stack Overflow用户
提问于 2021-11-21 09:45:56
回答 3查看 193关注 0票数 0

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

代码语言:javascript
代码运行次数:0
复制
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")
代码语言:javascript
代码运行次数:0
复制
elif choice == 2:
        instagram.getFollowing()
        following = open("following.txt", "r")
        for i in following:
            print(i)

输入您的COISE: 2

代码语言:javascript
代码运行次数:0
复制
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'
EN

回答 3

Stack Overflow用户

发布于 2021-11-21 09:55:33

2件事

您将获得

DeprecationWarning: find_element_by_*命令在中已弃用

为了解决这个问题,您应该更改所有

代码语言:javascript
代码运行次数:0
复制
find_element_by_* 

命令到

代码语言:javascript
代码运行次数:0
复制
find_element(By.XPATH, "XPATH")

或发送到By.CSS_SELECTOR、By.ID等。这取决于你的选择器。

  1. 没有这样的文件

'following.txt:FileNotFoundError: Errno 2没有这样的文件或目录:Errno

您正在使用此行代码

代码语言:javascript
代码运行次数:0
复制
with open("following.txt", "w", encoding="UTF-8") as file:

你给了我

代码语言:javascript
代码运行次数:0
复制
following.txt

由于它不是full file path,Python将查看您运行程序的当前项目内部。

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

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

在windows系统上,它应该是这样的:

代码语言:javascript
代码运行次数:0
复制
D:/FolderName/following.txt

这只是一个例子。

票数 1
EN

Stack Overflow用户

发布于 2021-11-21 10:03:45

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

代码语言:javascript
代码运行次数:0
复制
    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脚本中读取和写入文件。如果它可以工作,这意味着在你的主代码中,文件的位置是不同的。

票数 0
EN

Stack Overflow用户

发布于 2021-11-21 13:00:10

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

中找到有关如何抑制它的详细讨论

此错误消息...

代码语言:javascript
代码运行次数:0
复制
FileNotFoundError: [Errno 2] No such file or directory: 'following.txt'

...implies您的程序无法在当前工作目录中找到文件following.txt

代码语言:javascript
代码运行次数:0
复制
c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT

解决方案

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

确保在c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT sub-directory.中放置了所需的文本文件

  • 您还可以按如下方式传递所需文本文件following.txt的绝对路径:

使用open("c:/absolute_file_location/following.txt","w",encoding="UTF-8")作为文件:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70053378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档