python make curl request --- curl --location --request POST \…
- User
python
make curl request
curl --location --request POST
'https://staging.k.avito.ru/service-sitemap/links/'
--header 'X-Source: astereshko'
--header 'Content-Type: application/json'
--data-raw '{ "categoryId": 0, "page": 0, "type": null }' - Assistant
To make a cURL request in Python, you can use the
requestslibrary, which is popular for handling HTTP requests. Here's how you can make a POST request with the given cURL configuration:pythonimport requests import json url = 'https://staging.k.avito.ru/service-sitemap/links/' headers = { 'X-Source': 'astereshko', 'Content-Type': 'application/json' } data = { "categoryId": 0, "page": 0, "type": None } response = requests.post(url, headers=headers, data=json.dumps(data)) # Print the response status code and JSON content, if any print('Status Code:', response.status_code) try: print('Response:', response.json()) except json.JSONDecodeError: print('Response content is not JSON:', response.text)Key Points:
- User
print('Response:', response.json())
- print only 100 strings
- Assistant
If you want to print only the first 100 characters of the JSON response, you can slice the string representation of the response. Here's how you can modify the code to do that:
pythonimport requests import json url = 'https://staging.k.avito.ru/service-sitemap/links/' headers = { 'X-Source': 'astereshko', 'Content-Type': 'application/json' } data = { "categoryId": 0, "page": 0, "type": None } response = requests.post(url, headers=headers, data=json.dumps(data)) # Print the response status code print('Status Code:', response.status_code) # Attempt to print the first 100 characters of the JSON response try: response_content = response.json() response_string = json.dumps(response_content) # Convert the JSON to a string print('Response (first 100 characters):', response_string[:100]) except json.JSONDecodeError: print('Response content is not JSON:', response.text[:100]) # Print first 100 characters of non-JSON text - User
- make request function
with category as int, page as int
-
make cycle for i = 1...27 , j = 1..25
-
save result jsons to dataframe
url = 'https://prod.k.avito.ru/service-sitemap/links/' headers = { 'X-Source': 'astereshko', 'Content-Type': 'application/json' }
data = { "categoryId": 27, "page": 1, "type": None }
response = requests.post(url, headers=headers, data=json.dumps(data), cert=(cert_path, key_path))
Print the response status code and JSON content, if any
print('Status Code:', response.status_code) try: response_content = response.json() response_string = json.dumps(response_content) # Convert the JSON to a string print('Response (first 100 characters):', response_string[:1000]) except json.JSONDecodeError: print('Response content is not JSON:', response.text[:1000]) # Print first 100 characters of non-JSON text
- Assistant
To achieve the functionality you described, we'll define a function to make requests with specified categories and pages. We'll then iterate through the desired ranges for and , store the results in a list, and finally convert that list into a DataFrame using .
- User
result is json
{'links': ['/abadzehskaya/odezhda_obuv']}
parse json by key links, and make DF
- Assistant
To parse the JSON responses and extract the values for the key
"links", you can modify the code to iterate through the list of links and store each in the DataFrame. Here's how you can do it:python