[英]Python requests library: looping requests.get()
I have a script which I want to get data from an api every 30s. 我有一个脚本,希望每30秒从api获取数据。 I am using the requests module however every loop seems to return the same data.
我正在使用请求模块,但是每个循环似乎都返回相同的数据。 See the below "sample code":
请参见下面的“示例代码”:
import requests
from something import dosomething
http_proxy = "http://user:password@someproxy.com:8080"
https_proxy = "http://user:password@someproxy.com:8080"
ftp_proxy = "http://user:password@someproxy.com:8080"
proxyDict = {"http" : http_proxy, "https" : https_proxy, "ftp" : ftp_proxy}
url = "someapi.com"
g=requests.get(url,proxies=proxyDict)
try:
keep_running = True
while keep_running:
try:
dosomething(g.content)
So I need to tell requests to close a session or something like that? 所以我需要告诉请求关闭会话或类似的内容?
解决方案是在循环开始时使用.sessions
语句,并在循环结束时使用.close
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.