循环遍历 API 调用函数的多个 URL
Looping through multiple URLs for an API call function
我是 python 循环的新手,需要一些帮助。我在下面有一个网址列表。每个 URL 中的数字代表不同的产品 ID,对于每个产品 ID(1、2、3 等),脚本运行价格更新 api 调用。
www.random.com/product/1/offer
www.random.com/product/2/offer
www.random.com/product/3/offer
我需要能够遍历每个 URL 并为每个产品 ID (1,2,3) 执行相同的任务 - 目前我将其设置如下,但想知道我如何将 for 循环集成到其中。当我尝试使用数组 + for 循环时,脚本在调用 URL 时只执行数组内的整个值。下面是我的单个 URL.
代码
URL = 'www.random.com/product/1/offer'
ua = UserAgent()
#print(ua.chrome)
header = {'User-Agent':str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json', 'Authorization': 'xxx'}
def main():
response = requests.get(URL, headers=header)
response_formatted = json.loads(response.content.decode('utf-8-sig').encode('utf-8'))
price1 = response_formatted[0]["salePrice"]
print(price1)
discount_by=0.10
new_discount_price= (price1-discount_by)
print(new_discount_price)
sku = response_formatted[0]["sku"]
所以基本上我想让 'URL' 变量等于一堆不同的 URLs 并为每个唯一的 URL 值循环主调用。
我是 python 和 Whosebug 的新手,但我最近这样做了,所以我想我可以帮助你!!
我在一个函数中完成了所有工作,我确信它不是最好的,但它确实有效。我所做的是将我想要更改的所有值放入列表中。
你的情况:
id=[1,2,3]
然后在函数中加入ua和headers,用for循环做一个循环:
def main():
listid=[1,2,3]
for i in listid:
idlist=i
ua = UserAgent()
# print(ua.chrome)
header = {'User-Agent': str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json',
'Authorization': 'xxx'}
conn.request(f"GET", f"www.random.com/product/{idlist}/offer", headers=headers)
res = conn.getresponse()
data = res.read()
data_formatted = json.loads(data.content.decode('utf-8-sig').encode('utf-8'))
price1 = data_formatted[0]["salePrice"]
print(price1)
discount_by = 0.10
new_discount_price = (price1 - discount_by)
print(new_discount_price)
sku = data_formatted[0]["sku"]
我不知道它是否有效,但我相信如果它错了,有人会修复它,我希望我能帮到你!!
感谢阿德里安,我明白了。解决方案是:
def main():
listid=[1,2]
for i in listid:
idlist=i
URL=f'https://www.random.com/products/{idlist}/offers'
ua = UserAgent()
#print(ua.chrome)
header = {'User-Agent':str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json', 'Authorization': 'xxx'}
response = requests.get(URL, headers=header)
response_formatted = json.loads(response.content.decode('utf-8-sig').encode('utf-8'))
我是 python 循环的新手,需要一些帮助。我在下面有一个网址列表。每个 URL 中的数字代表不同的产品 ID,对于每个产品 ID(1、2、3 等),脚本运行价格更新 api 调用。
www.random.com/product/1/offer
www.random.com/product/2/offer
www.random.com/product/3/offer
我需要能够遍历每个 URL 并为每个产品 ID (1,2,3) 执行相同的任务 - 目前我将其设置如下,但想知道我如何将 for 循环集成到其中。当我尝试使用数组 + for 循环时,脚本在调用 URL 时只执行数组内的整个值。下面是我的单个 URL.
代码 URL = 'www.random.com/product/1/offer'
ua = UserAgent()
#print(ua.chrome)
header = {'User-Agent':str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json', 'Authorization': 'xxx'}
def main():
response = requests.get(URL, headers=header)
response_formatted = json.loads(response.content.decode('utf-8-sig').encode('utf-8'))
price1 = response_formatted[0]["salePrice"]
print(price1)
discount_by=0.10
new_discount_price= (price1-discount_by)
print(new_discount_price)
sku = response_formatted[0]["sku"]
所以基本上我想让 'URL' 变量等于一堆不同的 URLs 并为每个唯一的 URL 值循环主调用。
我是 python 和 Whosebug 的新手,但我最近这样做了,所以我想我可以帮助你!!
我在一个函数中完成了所有工作,我确信它不是最好的,但它确实有效。我所做的是将我想要更改的所有值放入列表中。
你的情况:
id=[1,2,3]
然后在函数中加入ua和headers,用for循环做一个循环:
def main():
listid=[1,2,3]
for i in listid:
idlist=i
ua = UserAgent()
# print(ua.chrome)
header = {'User-Agent': str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json',
'Authorization': 'xxx'}
conn.request(f"GET", f"www.random.com/product/{idlist}/offer", headers=headers)
res = conn.getresponse()
data = res.read()
data_formatted = json.loads(data.content.decode('utf-8-sig').encode('utf-8'))
price1 = data_formatted[0]["salePrice"]
print(price1)
discount_by = 0.10
new_discount_price = (price1 - discount_by)
print(new_discount_price)
sku = data_formatted[0]["sku"]
我不知道它是否有效,但我相信如果它错了,有人会修复它,我希望我能帮到你!!
感谢阿德里安,我明白了。解决方案是:
def main():
listid=[1,2]
for i in listid:
idlist=i
URL=f'https://www.random.com/products/{idlist}/offers'
ua = UserAgent()
#print(ua.chrome)
header = {'User-Agent':str(ua.chrome)}
headers2 = {'Content-Type': 'application/json', 'charset': 'utf-8', 'Accept': 'application/json', 'Authorization': 'xxx'}
response = requests.get(URL, headers=header)
response_formatted = json.loads(response.content.decode('utf-8-sig').encode('utf-8'))