课文末尾有一个算法。它从文件SP500.txt中读取行。文件包含字符串,看起来像:
AAA
BBB
CCC
替换get请求中的这些字符串,并将整个url保存到文件url\u requests.txt。例如:
https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d
https://apidate.com/api/api/BBB.US?api_token=XXXXXXXX&period=d
https://apidate.com/api/api/CCC.US?api_token=XXXXXXXX&period=d
然后通过API处理每个请求,并将所有响应添加到responses.txt。 我不知道如何将来自文件url\u requests.txt的每个请求的响应保存到单独的csv文件中,而不是responses.txt(现在它们都写入此文件,而不是单独写入)。在这种情况下,使用文件SP500.txt中的对应行命名每个文件非常重要。例如:
AAA.csv `(which contains data from the request response https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d)`
BBB.csv `(which contains data from the request response https://apidate.com/api/api/BBB.US?api_token=XXXXXXXX&period=d)`
CCC.csv `(which contains data from the request response https://apidate.com/api/api/CCC.US?api_token=XXXXXXXX&period=d)`
所以,算法是:
import requests
# to use strip to remove spaces in textfiles.
import sys
# two variables to squeeze a string between these two so it will become a full uri
part1 = 'https://apidate.com/api/api/'
part2 = '.US?api_token=XXXXXXXX&period=d'
# open the outputfile before the for loop
text_file = open("url_requests.txt", "w")
# open the file which contains the strings
with open('SP500.txt', 'r') as f:
for i in f:
uri = part1 + i.strip(' \n\t') + part2
print(uri)
text_file.write(uri)
text_file.write("\n")
text_file.close()
# open a new file textfile for saving the responses from the api
text_file = open("responses.txt", "w")
# send every uri to the api and write the respones to a textfile
with open('url_requests.txt', 'r') as f2:
for i in f2:
uri = i.strip(' \n\t')
batch = requests.get(i)
data = batch.text
print(data)
text_file.write(data)
text_file.write('\n')
text_file.close()
我知道如何从这个响应中保存csv。就像:
import csv
import requests
url = "https://apidate.com/api/api/AAA.US?api_token=XXXXXXXX&period=d"
response = requests.get(url)
with open('out.csv', 'w') as f:
writer = csv.writer(f)
for line in response.iter_lines():
writer.writerow(line.decode('utf-8').split(','))
要以不同的名称保存,在读取数据时必须在
for
内使用open()
和write()
-loop最好读取所有要列出的名称,然后生成URL,并将其保留在列表中,这样您就不必阅读它们
当我看到您用来保存}一次性保存所有代码,而无需使用
csv
的代码时,它看起来像是从服务器上获取csv
,这样您就可以使用open()
{csv
模块我是这样看的
您可以为每个字符串i计算一个文件名,并每次打开(创建)一个文件
大概是这样的:
您还可以去掉url_requests.csv,这将变得毫无用处(除非您有其他用途)
相关问题 更多 >
编程相关推荐