Requests – POST pagination?
Question:
i want to get these transactions:
https://www.omniexplorer.info/address/1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA
The first page is no problem with:
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA')]
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/', headers=headers, data=data)
response = response.json()
print(response["transactions"])
But how can i call page 2 for example?
I tried with params “params = {‘page’: 2}” but that doesnt work
Would appreciate any help!
regards
Answers:
You shoud think it maybe RESTful,then you will know how to do it
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
pj = {}
for page in range(1,3):
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA'),('page',page)]
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/', headers=headers , data = data)
response = response.json()
print(response)
pj[page] = response["transactions"]
value = list(pj.values())
print(value[0] == value[1])
For the API you’re using, you should send the page number as a form value:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "page=19" "https://api.omniexplorer.info/v1/properties/gethistory/3"
If you replace page=19 with page=20 you will see that the second call only has three entries, whereas the first has ten.
Using requests, that should be something like this:
r = requests.post('https://api.omniexplorer.info/v1/properties/gethistory/3',
data={'page': 10})
Or, using your own example rather than the one I found on their page:
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
'addr': '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA',
'page': 1,
}
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/',
headers=headers, data=data)
I have a similar request to get all companies from an API. In my case would like to get all companies, but the result set with the below query produces the following error.
error internal_message
None message
too many results type
RESULTLIST_TO_LARGE
Now I am looking into paginating the request, but I cannot find a way to do it. The API documentation is here
import pandas as pd
import requests
from urllib import response
import requests
import pandas as pd
import json
def company_load():
url = 'https://www.zefixintg.admin.ch/ZefixPublicREST/api/v1/company/search'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic <*password*>'
}
payload = json.dumps({
"activeOnly": True
})
r= requests.post(url, headers=headers, data= payload)
response = r.json()
full_companies= pd.DataFrame(response)
return full_companies
companies_df= company_load()
Improved offered solution on pagination loop.
import requests
import json
import pandas as pd
from pandas import json_normalize
url = 'https://api.omniexplorer.info/v1/address/addr/details/'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
page = 1
data_json = []
while data_json is not None:
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA'),('page',page)]
response = requests.post(url, headers=headers , data = data)
data_json = response.json()['transactions']
print(pd.DataFrame.from_dict(data_json))
if data_json is None:
break
page += 1
i want to get these transactions:
https://www.omniexplorer.info/address/1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA
The first page is no problem with:
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA')]
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/', headers=headers, data=data)
response = response.json()
print(response["transactions"])
But how can i call page 2 for example?
I tried with params “params = {‘page’: 2}” but that doesnt work
Would appreciate any help!
regards
You shoud think it maybe RESTful,then you will know how to do it
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
pj = {}
for page in range(1,3):
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA'),('page',page)]
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/', headers=headers , data = data)
response = response.json()
print(response)
pj[page] = response["transactions"]
value = list(pj.values())
print(value[0] == value[1])
For the API you’re using, you should send the page number as a form value:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "page=19" "https://api.omniexplorer.info/v1/properties/gethistory/3"
If you replace page=19 with page=20 you will see that the second call only has three entries, whereas the first has ten.
Using requests, that should be something like this:
r = requests.post('https://api.omniexplorer.info/v1/properties/gethistory/3',
data={'page': 10})
Or, using your own example rather than the one I found on their page:
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
'addr': '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA',
'page': 1,
}
response = requests.post('https://api.omniexplorer.info/v1/address/addr/details/',
headers=headers, data=data)
I have a similar request to get all companies from an API. In my case would like to get all companies, but the result set with the below query produces the following error.
error internal_message
None message
too many results type
RESULTLIST_TO_LARGE
Now I am looking into paginating the request, but I cannot find a way to do it. The API documentation is here
import pandas as pd
import requests
from urllib import response
import requests
import pandas as pd
import json
def company_load():
url = 'https://www.zefixintg.admin.ch/ZefixPublicREST/api/v1/company/search'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic <*password*>'
}
payload = json.dumps({
"activeOnly": True
})
r= requests.post(url, headers=headers, data= payload)
response = r.json()
full_companies= pd.DataFrame(response)
return full_companies
companies_df= company_load()
Improved offered solution on pagination loop.
import requests
import json
import pandas as pd
from pandas import json_normalize
url = 'https://api.omniexplorer.info/v1/address/addr/details/'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
page = 1
data_json = []
while data_json is not None:
data = [('addr', '1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA'),('page',page)]
response = requests.post(url, headers=headers , data = data)
data_json = response.json()['transactions']
print(pd.DataFrame.from_dict(data_json))
if data_json is None:
break
page += 1