提取JSON值并使用它

huangapple go评论53阅读模式
英文:

extracting JSON value and using it

问题

我发送了简单的请求,并收到了JSON响应:

{'status': 'success', 'orders': [{'id': '1666543', 'link': 'asd.com', 'quantity': '111'}]}

我的响应代码:

response = requests.get(url, params=params)
response.json()
wynik = response.json()
print(response.json())
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(response.text, f, ensure_ascii=False, indent=4)

它保存在data.json中,但是在一行里。

我需要的是,在我的Selenium脚本中使用'link'值。
所以整个概念是要实现100%自动化。
我发送一个请求,得到响应,然后在另一个脚本中使用响应中的'link'值。

我不知道如何提取链接值。

我将非常感谢您的帮助。

英文:

I am sending simple request and I am getting JSON response:

{'status': 'success', 'orders': [{'id': '1666543', 'link': 'asd.com', 'quantity': '111'}]}  

My code for response:

response = requests.get(url, params=params)
response.json()
wynik = response.json()
print(response.json())
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(response.text, f, ensure_ascii=False, indent=4)

It saves to data.json but in one line.

What I need is, to use the 'link' value in my selenium script.
So the whole concept is to have it 100% automated.
I am sending a request, I am getting the response and I am using this response value of 'link' in another script.

I have no idea how to extract the link value.

I will be grateful for help.

答案1

得分: 1

以下是已翻译的部分:

"If the data you're getting is the one you shared at the beginning of your question, then to extract the link :

link = wynik['orders'][0]['link']

This will work assuming you have one object in the orders array (it's an array, I hope you noticed that), if you have multiple objects and therefore want to extract multiple links, then you need a loop and that will be a different question that requires a different answer. If it's the case let me know."

英文:

If the data you're getting is the one you shared at the beginning of your question, then to extract the link :

link = wynik['orders'][0]['link']

This will work assuming you have one object in the orders array (it's an array, I hope you noticed that), if you have multiple objects and therefore want to extract multiple links, then you need a loop and that will be a different question that requires a different answer. If it's the case let me know.

huangapple
  • 本文由 发表于 2023年3月3日 22:08:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定