![](/img/trans.png)
[英]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.