我试图遍历一组项目并在每个项目上执行curl。
我已经成功实现了它,但是似乎它一次执行了所有操作,而有些项目却被跳过了。
我看到的是它不等待第一个curl的响应,而是开始执行另一个。
我已经在 python 中实现了同样的功能,效果很好。它一次执行一个请求
import requests, var_dump, csv
filepath = 'information.txt'
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
print (line)
headers = {
'Accept': 'application/json'
}
params = (
('search-term', line),
)
response = requests.get('https://url.tld/entities', headers=headers, params=params)
line = fp.readline()
cnt += 1
with open('names.csv', 'a') as csvfile:
fieldnames = ['search', 'json']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'search': line.encode("utf-8"), 'json': response.text.encode("utf-8")})
print(response.text)
print(cnt)