繁体   English   中英

如何使用 Python 中的大量值发出 API 请求?

[英]How do I make an API request with a large set of values in Python?

I'm writing the Python code for a data server that downloads a vendor's entire product code list via an API call, checks inventory levels for each item in four different warehouses (another API call), and saves the results to a csv file. 获取产品的库存水平最多可以批量为 100 个。

这是超级慢但仅供参考,如果它是我正在执行 API 请求的单个部分,这就是我所拥有的:

def inventory_query(part_number):  # get inventory levels for each code
    try:
        api_config = read_config()["API"]
        host = api_config["host"]
        key = api_config["key"]

        url = "https://**********/api3/1/inventory"

        headers = {
            "X-API-HOST": host,
            "X-API-KEY": key
        }

        data = {
            "productCodes": [
                part_number
            ],
            "warehouseCodes": [
                "1000",
                "1100",
                "1200",
                "1300"
            ]
        }

        response = requests.post(url, json=data, headers=headers)
        soup = BeautifulSoup(response.text, features='html.parser')
        text = soup.get_text(separator='\n').split('\n')

        return str(text[0]), str(text[6]), str(text[3]), str(text[9])
        
    except ConnectionResetError:
        time_now = datetime.now()
        logging.debug(str(time_now) + " - Couldn't get inventory levels!")

我只在 Python 编程一年多一点,所以我几乎不知道part_number但我觉得有比一次列出 100 个代码(名为“parts”)并用str(parts[0]), str(parts[1]), str(parts[2]), etc...到 POST 数据。

有没有更好的办法?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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