Home > other >  How to get data some table data in a url using python
How to get data some table data in a url using python

Time:10-25

I am trying to get some data in a website but my code is not really working. I am new to python and I am just trying to get some code working. Any idea or help to make this work is very welcome. Thanks in advance.

import requests
from bs4 import BeautifulSoup

headers = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:92.0) Gecko/20100101 Firefox/92.0",}

url = "https://minepi.com/blockexplorer/account/GDOI7OSBDM3WPXWFT3RUPGGLV3Y5MKZJKNN4CZMQITUWWQRUF5IXCLZJ"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
t = soup.find("table", class_="table")
trs = t.find("tbody").find_all("tr")

for tr in trs:
    print(list(tr.stripped_strings))

Current result:

Traceback (most recent call last):
  File "C:/Users/estoryahe/Desktop/Pi Scripts/pi-temp1.py", line 14, in <module>
    trs = t.find("tbody").find_all("tr")
AttributeError: 'NoneType' object has no attribute 'find'

Need result:

Public Key: GDOI7OSBDM3WPXWFT3RUPGGLV3Y5MKZJKNN4CZMQITUWWQRUF5IXCLZJ
Balance: 26.9231952

CodePudding user response:

There's an API you can use to get the data:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0',
}

public_key = "GDOI7OSBDM3WPXWFT3RUPGGLV3Y5MKZJKNN4CZMQITUWWQRUF5IXCLZJ"
url = f"https://api.testnet.minepi.com/accounts/{public_key}"

response = requests.get(url, headers=headers).json()
balances = response['balances']
for balance in balances:
    print(f"Public Key: {response['account_id']}")
    print(f"Balance: {balance['balance']}")

Note: for the key you've provided this is the result

Public Key: GDOI7OSBDM3WPXWFT3RUPGGLV3Y5MKZJKNN4CZMQITUWWQRUF5IXCLZJ
Balance: 100.0000000
  • Related