python - 目录中没有该文件

标签 python selenium-webdriver web-scraping automation selenium-chromedriver

我是 Python 新手,我需要传递 URL 列表作为脚本的输入,我使用 f1=open 进行读取,使用 f2=open 进行写入输出

from selenium import webdriver
import selenium
import time
import bs4
import sys



f1=open("links.txt","r")
urls=f1.readlines()
urls=[urls.split()[0] for x in urls]
f1.close()

f2=open("a.txt","w")
for url in urls:
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(5)
    print(driver.current_url)
    f2.write(str(driver.current_url)+"\n")

f2.close()

但是我遇到了这个错误

FileNotFoundError:[Errno 2]没有这样的文件或目录:“links.txt”

请帮助我

最佳答案

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

错误提到该文件不存在于目录中。

请注意,您需要检查文件名。我相信您已经通过右键单击新文本文件创建了文本文件,然后将其命名为 links.txt 因此,当您打开文件名时,文件名更改为 links.txt.txt只需按照快照中所述另存为此文件即可。

enter image description here

并尝试以下选项。

f1=open("links.txt","r")

或者您可以不保存为上面提到的文件。

f1=open("links.txt.txt","r")

或者您可以使用文件的完整路径来打开文件。

f1=open(r"D:\Foldername\links.txt.txt","r")

或者

f1=open(r"D:\Foldername\links.txt","r")

请告诉我此解决方法是否适合您。

关于python - 目录中没有该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59469795/

相关文章:

javascript - 使用 Cheerio 抓取网页

python - anaconda virtual env 从全局导入模块

java - org.openqa.selenium.json.JsonOutput.write(Ljava/lang/Object;Ljava/lang/reflect/Type;) 由于 JsonException : Attempting to close incomplete json stream

node.js - 获取浏览器的当前 URL

xpath - 如何捕获以下代码的文本值?

ruby - 访问 div 元素数组中的 div 元素的属性

python - Python 2.7 中的 `id` 函数,`is` 运算符、对象标识和用户定义的方法

python - 为什么 ipywidget 按钮调用的 my 函数不返回输出?

python - 使用嵌套列表迭代字典并在板位置打印

java - 如何开始构建基于 Java 的网络抓取工具