【问题标题】:How to open links from a txt file in python?如何在python中打开txt文件的链接?
【发布时间】:2019-07-01 15:40:38
【问题描述】:

我已经设法创建了一个 txt 文件,其中列出了我想要打开并从中获取信息的所有链接的列表,但我无法找到打开这些链接以扫描 html 的方法。任何帮助将非常感激!我是python的初学者,所以请尽可能简单地写下任何答案,因为我可能无法理解它:)

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow,我们不会为您编写代码,但我们可以帮助您解决您可能遇到的任何错误/困难。请编辑您的帖子并添加您到目前为止编写的代码以检索 .txt 文件

标签: python html url hyperlink


【解决方案1】:

首先您要使用 open() 打开 txt 文件。

fp = open('path/to/file.txt')

然后,对于该 txt 文件中的每一行,您可以循环遍历每一行。

for line in fp.readlines():
    print line

#ensure to close the file after
fp.close() 

此外,您始终可以将每一行附加到列表中以供以后使用。

myList=[]
fp = open('path/to/file.txt')
for line in fp.readlines():
    myList.append(line)
fp.close() 

这也是一种打开链接的方法。

import webbrowser

for i in myList:
    webbrowser.open(myList[i])

这里有一些关于readlines ()open() & close() files 的教程

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 2022-10-13
    • 1970-01-01
    • 2020-03-07
    相关资源
    最近更新 更多