Python 请求库:循环 requests.get() [英] Python requests library: looping requests.get()

查看:134
本文介绍了Python 请求库:循环 requests.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我想每 30 秒从一个 api 获取数据.我正在使用请求模块,但是每个循环似乎都返回相同的数据.请参阅下面的示例代码":

I have a script which I want to get data from an api every 30s. 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 语句在循环结束时.

The solution was to use .sessions statement at the beginning of loop and use the .close statement at the end of the loop.

这篇关于Python 请求库:循环 requests.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆