循环浏览不同的熊猫系列以获得请求



一个api需要两个不同的参数,所以我想遍历这些存储在pandas系列中的参数,因此我想找到一种更好地遍历它们的方法,以便同时请求它们。

这是API请求:

url = f"https://api.opensea.io/api/v1/asset/a/b/listings?limit=50"
headers = {
"Accept": "application/json",
"X-API-KEY": "{key}"
}
response = requests.request("GET", url, headers=headers)
response

ab是指我要插入的参数。

a           b
5355        0x2efa07cac3395599db83035d196f2a0e7263f766
22591       0x60e4d786628fea6478f785a6d7e704777c86a7c6
165         0x3a5051566b2241285be871f650c445a88a970edd
12971       0x9ca8887d13bc4591ae36972702fdf9de2c97957f
6706        0x521f9c7505005cfa19a8e5786a9c3c9c9f5e6f42
6230        0x8a90cab2b38dba80c64b7734e58ee1db38b8992e
2746        0xc7df86762ba83f2a6197e1ff9bb40ae0f696b9e6
803         0x177ef8787ceb5d4596b6f011df08c86eb84380dc
5682        0x2acab3dea77832c09420663b0e1cb386031ba17b
7473        0xba30e5f9bb24caa003e9f2f0497ad287fdf95623

因此,我想要一种方法来遍历a和b来发出请求,这样a和b的每一行都会被请求在一起,并且不会混淆。

使用iterrows:

# Remove the f-strings
url = "https://api.opensea.io/api/v1/asset/{a}/{b}/listings?limit=50"
headers = {
"Accept": "application/json",
"X-API-KEY": "becbd5981e0c482083b3bac4267ad651"
}
for _, row in df.iterrows():
# response = requests.request("GET", url.format(**row.to_dict()), headers=headers)
# print(response)
print(url.format(**row.to_dict()))

输出:

https://api.opensea.io/api/v1/asset/5355/0x2efa07cac3395599db83035d196f2a0e7263f766/listings?limit=50
https://api.opensea.io/api/v1/asset/22591/0x60e4d786628fea6478f785a6d7e704777c86a7c6/listings?limit=50
https://api.opensea.io/api/v1/asset/165/0x3a5051566b2241285be871f650c445a88a970edd/listings?limit=50
https://api.opensea.io/api/v1/asset/12971/0x9ca8887d13bc4591ae36972702fdf9de2c97957f/listings?limit=50
https://api.opensea.io/api/v1/asset/6706/0x521f9c7505005cfa19a8e5786a9c3c9c9f5e6f42/listings?limit=50
https://api.opensea.io/api/v1/asset/6230/0x8a90cab2b38dba80c64b7734e58ee1db38b8992e/listings?limit=50
https://api.opensea.io/api/v1/asset/2746/0xc7df86762ba83f2a6197e1ff9bb40ae0f696b9e6/listings?limit=50
https://api.opensea.io/api/v1/asset/803/0x177ef8787ceb5d4596b6f011df08c86eb84380dc/listings?limit=50
https://api.opensea.io/api/v1/asset/5682/0x2acab3dea77832c09420663b0e1cb386031ba17b/listings?limit=50
https://api.opensea.io/api/v1/asset/7473/0xba30e5f9bb24caa003e9f2f0497ad287fdf95623/listings?limit=50

最新更新