[英]Looping through multiple URLs for an API call function
I'm fairly new to python loops and need some assistance.我是 python 循环的新手,需要一些帮助。 I have a list of urls below.
我在下面有一个网址列表。 The numbers inside each of the URL represent a different product ID, and for each product ID(1,2,3 etc), the script runs a price update api call.
每个 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
I need to be able to loop through each of the URLs and perform the same task for each product id (1,2,3) - currently I have it setup as the following, but wondering how I can integrate a for loop into this.我需要能够遍历每个 URL 并为每个产品 ID (1,2,3) 执行相同的任务 - 目前我将其设置如下,但想知道如何将 for 循环集成到其中。 When I try with arrays + for loop it the script just executes the entire value inside the array when it calls the URL. Below is my code for a single URL.
当我尝试使用 arrays + 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"]
So basically I would like to have 'URL' variable equal to a bunch of different URLs and loop the main call for each unique URL value.所以基本上我想让“URL”变量等于一堆不同的 URL,并为每个唯一的 URL 值循环主调用。
Im new in this of python and stackoverflow but i recently did that so i think i can help you.!.我是 python 和 stackoverflow 的新手,但我最近这样做了,所以我想我可以帮助你。!
I did all in a function, im sure its not the best but it works.我在 function 中完成了所有工作,我确定它不是最好的,但它确实有效。 What i did is put all the values that i wanna change in a list.
我所做的是将我想要更改的所有值放入列表中。
In your case:在你的情况下:
id=[1,2,3]
Then add ua and headers in the function and make a loop with the for loop:然后在function中加入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"]
I dont know if it works but im sure someone will fix it if its wrong, i hope i helped you!!我不知道它是否有效,但我相信如果它错了,有人会修复它,我希望我能帮到你!!
Thanks to Adrian, I got it.感谢阿德里安,我明白了。 Solution was:
解决方案是:
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'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.