[英]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.