在网页中搜索一个单词并在Python中保存为TXT



我正在尝试:从。txt文件加载链接,搜索特定的Word,如果该单词存在于该网页上,将链接保存到另一个。txt文件,但我得到错误:No scheme supplied. Perhaps you meant http://<_io.TextIOWrapper name='import.txt' mode='r' encoding='cp1250'>?注:链接有HTTPS://

代码:

import requests
list_of_pages = open('import.txt', 'r+')
save = open('output.txt', 'a+')
word = "Word"
save.truncate(0)
for page_link in list_of_pages:
res = requests.get(list_of_pages)
if word in res.text:
response = requests.request("POST", url)
save.write(str(response) + "n")

有谁能解释为什么吗?提前谢谢你!

试试在链接后面加上http://。

当您使用res = requests.get(list_of_pages)时,您正在创建到list_of_pages的HTTP连接。但是requests.get将URL字符串作为参数(例如http://localhost:8080/static/image01.jpg),看看list_of_pages是什么-它是一个已经打开的文件。不是字符串。你必须要么使用请求库,要么使用文件IO API,而不是两者都使用。

如果你有一个已经打开的文件,你根本不需要创建HTTP请求。你不需要这个request.get()。解析list_of_pages像一个普通的本地文件。

或者,如果你想走另一种方式,不要在list_of_arguments中打开这个文本文件,让它成为一个带有该文件URL的字符串。

最新更新