'python script returns multiple data, how to get the first data and download it
My script is
import requests
from requests.auth import HTTPBasicAuth
import json
url = "https://your-domain.atlassian.net/rest/api/2/issue/{issueIdOrKey}"
auth = HTTPBasicAuth("[email protected]", "<api_token>")
headers = {
"Accept": "application/json"
}
response = requests.request(
"GET",
url,
headers=headers,
auth=auth
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
And get the response body, the format as below:
{
"startAt":0
"comment":[
{
"self": "xxxx",
"id": "xxx",
"body": "daily in URLlink"
},
{
"self": "xxxx",
"id": "xxx",
"body": "daily in URLlink"
},
...
]
}
And I need to get the first data in the comment(The URLlink in the body).
The URLlink is download link, I want to download the URLlink content automatically by script.
Update1
import requests
from requests.auth import HTTPBasicAuth
import json
url = "https://your-domain.atlassian.net/rest/api/2/issue/{issueIdOrKey}"
auth = HTTPBasicAuth("[email protected]", "<api_token>")
headers = {
"Accept": "application/json"
}
response = requests.request(
"GET",
url,
headers=headers,
auth=auth
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
url = responsed["comment"][0]["body"]
r = requests.get(url, allow_redirects=True)
open('file_name', 'wb').write(r.content)
And get the error:
Then I add import responsed
, get the latest error.
Update2
Now, update the python script, it as below:
import requests
from requests.auth import HTTPBasicAuth
import json
#import responsed
url = "https://your-domain.atlassian.net/rest/api/2/issue/{issueIdOrKey}"
auth = HTTPBasicAuth("[email protected]", "api_token")
headers = {
"Accept": "application/json"
}
response = requests.request(
"GET",
url,
headers=headers,
auth=auth
)
#print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
url = response.json()['comments'][0]["body"]
And it response body format like:
daily build in URLDownloadLink
Before this, I need to click URLDownloadLink
download it.
And now, I want to achieve automatic download. Thx
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|