简体   繁体   中英

Using Python to Loop Through Variables in an API Request

I want to use one script to make a series of API calls for a list of addresses/ids. The issue is that when I run the script, I eventually get a timeout. Othertimes the address variable embeddded into data_request isn't recognized. Below is my current code.

import requests
import json
import csv
import pandas as pd
import time

    with open('TransactionHistory.csv','a') as f: 
        while True:
            addresses=["0xe3457148b4f535b7a42bf6aaa06c6c92a0b9f9cb", "0xad8f33994ec88ab208ada1d286b41c4a39515aed"]
                for address in addresses:

                data_request=requests.get("http://api.etherscan.io/api? module=account&action=txlist&address="+address+"=5585000&endblock=5590000&sort=asc&apikey=SZAEWWTN9YXQNR4X593AUDB7Y5323D6CAE") 
                json=data_request.json()
                df = pd.DataFrame(json,dtype=object,index=[0])
                [df.to_csv(f, header=False) for i in range (1)] 
                time.sleep(120) 

You have a "while True:" in your code, so it looks like it will loop forever. Most public API's have a rate/call limit, and will timeout after you hit it. My guess is that you're hitting the API until it just stops answering you.

Try removing the "while True" loop, and stepping through your code to see if it logically makes sense.

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