简体   繁体   中英

Python requests library: looping requests.get()

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM