[英]How can I read a list of urls from a text file and save all the responses into one text file?
[英]How do you read URLs from a .txt file to perform requests.get and save the responses to a file?
我能够保存单个 URL 的响应,但我在 a.txt 文件中有一个 URL 列表,并且想打印所有响应。 如何从 .txt 文件中读取 URL 并将响应保存在 python 中?
这是我目前拥有的。 谢谢!
import requests
data = requests.get('www.url.com')
with open('file.txt', 'wb') as f:
f.write(data.text)
您首先要从文件“infile.txt”中读取 url,然后迭代地发送请求并将数据写入输出文件“outfile.txt”。
with open('infile.txt', 'r') as f:
urls = f.readlines()
datalist=[]
for url in urls:
data = requests.get(url)
datalist.append(data.text)
with open('outfile.txt', 'w') as f:
for item in datalist:
f.write("%s\n" % item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.