尝试在文本文件中循环播放电影的标题以调用API并将响应存储到文本文件中。
文件在每行ex上包含一个标题。
Sub Demo()
Dim xlWs As Worksheet, objWrd As Object, objDoc As Object, r As Long
Set xlWs = Sheets("Synonyms")
Set objWrd = CreateObject("Word.Application")
With objWrd
.Visible = False
Set objDoc = .Documents.Open("E:\Original.docm", False, False, False)
With objDoc.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = False
.MatchWholeWord = False
For r = 1 To xlWs.Cells(Rows.Count, "A").End(xlUp).Row
.Text = xlWs.Range("A" & r).Text
.Replacement.Text = xlWs.Range("B" & r).Text
.Execute Replace:=2 '2 = wdReplaceAll
Next
End With
objDoc.Close True
.Quit
End With
End Sub
我要使用的api来自www.odmpapi.com
这是我必须正确加入并创建Weblink的条件
Titanic
Avatar
A Star Is Born
这仅将列表中的最后一个标题写入response.txt。
答案 0 :(得分:1)
尝试以下几种方法:
import requests
import sys
base_url = 'http://www.omdbapi.com/?t={}&apikey=xxx4s23'
with open('print.txt', 'r') as f_input, open('responses.txt', 'w') as f_output:
for line in f_input:
search_term = line.strip(' \n\t')
url = base_url.format(search_term)
print url
batch = requests.get(url)
f_output.write("{},{}\n".format(url, batch.text))
这会将URL和结果写入输出文件。可以使用Python的.format()
命令将搜索时间放入base_url
中,而不必将其分解和使用字符串连接。通过用传递的参数替换每个{}
来工作。