简体   繁体   English

Python 请求:从一个 TXT 文件中获取所有行,一次获取一个请求并将它们保存到一个新的 TXT 文件中

[英]Python Requests: take all lines from a TXT file, one at a time to get requests from each and save them to a new TXT file

This code here gets a bunch of URLS from web archive.org and save them to a new TXT file.此处的代码从 web archive.org 获取一堆 URL,并将它们保存到一个新的 TXT 文件中。 I want instead of INPUT (write one url address), to load a bunch of URLS from a TXT file.我不想输入(写一个 url 地址),而是从 TXT 文件加载一堆 URLS。 So x=input('URL:') must be replaced with some code to load each line at a time from a txt file.所以x=input('URL:')必须用一些代码替换,以便一次从 txt 文件加载每一行。

I've been trying for few days now, i'm stuck!我已经尝试了几天了,我被卡住了! Please help!请帮忙!

The code:代码:

x=input('Enter your url:-')
r = requests.get('http://web.archive.org/cdx/search/cdx?url=*.{}&output=text&fl=original&collapse=urlkey'.format(x))
with open('url.txt', 'a') as f:
    f.write('\n')
    f.writelines(str(r.text))
    f.write('\n')

First, you need to have all URLs in urls.txt file each separated by new lines and then open it with the readlines() function. it will return the list of all URLs.首先,您需要将 urls.txt 文件中的所有 URL 都用新行分隔,然后使用 readlines() function 打开它。它将返回所有 URL 的列表。 here is the complete code.这是完整的代码。

import requests
with open('urls.txt') as file:
    # get the list of urls
    urls_list=file.readlines()
    for x in urls_list:
        r = requests.get('http://web.archive.org/cdx/search/cdx?url=*.{}&output=text&fl=original&collapse=urlkey'.format(x))
        print(r.status_code)

To read URLs from file you can use next example:要从文件中读取 URL,您可以使用下一个示例:

import requests

urls = []
with open("something.txt", "r") as f_in:
    for line in map(str.strip, f_in):
        if line == "":
            continue
        urls.append(line)

archive_url = "http://web.archive.org/cdx/search/cdx?url=*.{}&output=text&fl=original&collapse=urlkey"

with open("output.txt", "w") as f_out:
    for url in urls:
        print(url)
        r = requests.get(archive_url.format(url))
        print(r.text, file=f_out)
        print("\n", file=f_out)

something.txt contains domains, for example: something.txt包含域,例如:

google.com
yahoo.com

output.txt contains response from requests output.txt包含来自requests的响应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 txt 文件中一次一行地获取数据并将其用作发送请求的变量? - How can i take data one line at a time from a txt file and use it as a variable to send requests? 从python请求中的txt文件中的链接获取所有分页URL的列表 - Get list of all paginated URL's from links in txt file in python requests 尝试添加仅提取包含“word”的行的代码,并从请求中写入一个 new.txt 文件 - Trying to add code that extract only lines that contains "word" and write a new .txt file from requests 您如何从 a.txt 文件中读取 URL 以执行 requests.get 并将响应保存到文件中? - How do you read URLs from a .txt file to perform requests.get and save the responses to a file? 如何从txt文件中提取一列并保存在新矩阵中 - How to take a column from a txt file and save in a new matrix 如何从已拆分的.txt文件中获取多行并将其保存到不同的变量中 - How to get multiple lines from a .txt file that have been split and to save them into different variables Python:从.txt文件读取行并使用它们进行计算 - Python: Reading lines from .txt file and calculating with them 如何将所有 txt 文件从目录转换为 csv(创建和写入文件)并使用 Python 保存它们? - How to convert all the txt file from a dir to csv's (creating and writing to file )and save them using Python? 计算txt文件中每一行的出现次数,并将其保存到Python矩阵中 - Count the occurance in each line from a txt file and save them into matrix in Python 如何从 Spotipy 获取歌曲列表,然后将它们保存到 txt 文件? - How to get list of songs from Spotipy and then save them to a txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM