I am trying to get the data from this website.
https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&page=1&offset=100&sort=asc&apikey=YourApiKeyToken
However, I keep getting errors when I execute the following code
import pandas as pd
import json
import urllib.request
from urllib.request import FancyURLopener
url = 'https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&page='
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'}
request_interval = 2 # interval
urls = []
df = []
if __name__ == '__main__':
for i in range(1, 2):
url = urllib.parse.urljoin(url, '&page='+str(i)+'&offset=10000&sort=asc&apikey=YourApiKeyToken')
urls.append(str(url))
for url in urls:
headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"}
request = urllib.request.Request(url=url, headers=headers)
html = urllib.request.urlopen(request).read()
result = json.loads(html.decode('utf-8'))['blockNumber']
df.extend(json.loads(html.decode('utf-8'))['blockNumber'])
print('Completed URL : ', url)
pdf = pd.DataFrame(df)
pdf.to_csv("output.csv")
I have tried several solutions I found here Stackoverflow.
urllib2.HTTPError: HTTP Error 400: Bad Request - Python
urllib2 HTTP Error 400: Bad Request
I also have changed
headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"}
and
{'Authorization': auth,
'Content-Type':'application/json',
'Accept':'application/json'}
but still getting the same error.
Thank you