Reputation: 29
completely new to Python and I'm trying to get stuck in but I'm struggling with requests. I run a node for a small cryptocurrency project and am trying to create a python script that can scrape my wallet value and telegram it to me once a day, I've managed the telegram bot and I've practiced with BeautifulSoup to pull values out from a source fine, it's just getting a response that contains my balance that's frustrating me.
Here's the URL with my balance on: https://www.hpbscan.org/address/0x7EC332476fCA4Bcd20176eE06F16960b5D49333e/
The value obviously changes so I don't think I can just do a get request for the above page and parse it to beautiful soup, so I loaded up Developer Tools and saw that there was a post request:
METHOD: POST
URL: https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo
Request Headers:
Host: www.hpbscan.org
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: /
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate, br
X-Requested-With: XMLHttpRequest
Content-Type: application/json;charset=utf-8
Content-Length: 46
DNT: 1
Connection: keep-alive
Referer: https://www.hpbscan.org/address/0x7EC332476fCA4Bcd20176eE06F16960b5D49333e/
Pragma: no-cache
Cache-Control: no-cache
Request Body: ["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]
The response (at least in a browser) is JSON formatted data that does indeed contain the balance I need.
Here's where I got to so far trying to recreate the above request:
import requests
import json
url = "https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo"
payload = '["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]'
headers = """
'Host': 'www.hpbscan.org'
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'
'Accept': '*/*'
'Accept-Language': 'en-GB,en;q=0.5'
'Accept-Encoding': 'gzip, deflate, br'
'X-Requested-With': 'XMLHttpRequest'
'Content-Type': 'application/json;charset=utf-8'
'Content-Length': '46'
'DNT': '1'
'Connection': 'keep-alive'
'Referer': 'https://www.hpbscan.org/address/0x7EC332476fCA4Bcd20176eE06F16960b5D49333e/'
'Pragma': 'no-cache'
'Cache-Control': 'no-cache'
"""
data = requests.post(url, data=payload, headers=headers)
print(data.text)
I've never used requests before so I'm a bit in the dark, I've tried fiddling with things based on what I can see other people doing but it's no use, currently I'm getting "AttributeError: 'str' object has no attribute 'items'.
I'd imagine it to be something along the lines of me not specifying the request headers and body correctly, or maybe because the response is in json format which my code can't understand?
Any help would be massively appreciated :)
Upvotes: 2
Views: 216
Reputation: 815
The headers should be a dictionary
import requests
import json
url = "https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo"
payload = '["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]'
headers = {
'Host': 'www.hpbscan.org',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': '*/*',
'Accept-Language': 'en-GB,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json;charset=utf-8',
'Content-Length': '46',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://www.hpbscan.org/address/0x7EC332476fCA4Bcd20176eE06F16960b5D49333e/',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'}
data = requests.post(url, data=payload, headers=headers)
print(json.loads(data))
the final bit converts the response you get from the browser to a python dictionary so you can continue to make use of it within your code.
Upvotes: 0
Reputation: 1248
You should change "headers" from string to dict. Here your final code:
import requests
import json
url = "https://www.hpbscan.org/HpbScan/addrs/getAddressDetailInfo"
payload = '["0x7EC332476fCA4Bcd20176eE06F16960b5D49333e"]'
headers = {
'Host': 'www.hpbscan.org',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': '*/*',
'Accept-Language': 'en-GB,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json;charset=utf-8',
'Content-Length': '46',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://www.hpbscan.org/address/0x7EC332476fCA4Bcd20176eE06F16960b5D49333e/',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'}
data = requests.post(url, data=payload, headers=headers)
print(data.text)
Upvotes: 1