This snippet works fine last week as I was using it. When I tried running this today, it displays an error trs = t.find('tbody').find_all('tr').
import requests
from bs4 import BeautifulSoup
url = 'https://bscscan.com/token/generic-tokenholders2?m=normal&a=0xe56842ed550ff2794f010738554db45e60730371'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
t = soup.find('table', class_='table table-md-text-normal table-hover')
trs = t.find('tbody').find_all('tr')
for tr in trs[:5]:
print(list(tr.stripped_strings))
Current Output:
trs = t.find('tbody').find_all('tr')
AttributeError: 'NoneType' object has no attribute 'find'
CodePudding user response:
Try to add User-Agent
HTTP header to your request:
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://bscscan.com/token/generic-tokenholders2?m=normal&a=0xe56842ed550ff2794f010738554db45e60730371&s=1000000000000000000000000&sid=68a003f6aa7123d1ce19faee148486b1&p=1'"
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, "lxml")
t = soup.find("table", class_="table table-md-text-normal table-hover")
trs = t.find("tbody").find_all("tr")
for tr in trs[:5]:
print(list(tr.stripped_strings))
Prints:
['1', '0xbbda05ea467ad348212dade5c38c11910c14e83e', '202,543,296.764175197290329227', '20,254.3297%', '$3,054,207.08']
['2', 'UniCrypt: Token Vesting', '150,000,000', '15,000.0000%', '$2,261,892.00']
['3', 'PancakeSwap V2: BIN 17', '99,217,566.546415684406759104', '9,921.7567%', '$1,496,129.47']
['4', 'Binemon: BIN Token', '27,010,296.006072937294814033', '2,701.0296%', '$407,295.82']
['5', '0x81da471feb4a45438053dc05e709be056ec26c39', '14,865,062', '1,486.5062%', '$224,154.43']
CodePudding user response:
Actually, the table is not dynamic thay's why you can do that only using pandas.
Code:
import requests
import pandas as pd
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'}
url = "https://bscscan.com/token/generic-tokenholders2?m=normal&a=0xe56842ed550ff2794f010738554db45e60730371&s=1000000000000000000000000&sid=68a003f6aa7123d1ce19faee148486b1&p=1'"
req = requests.get(url,headers=headers)
wiki_table = pd.read_html(req.text, attrs = {"class":"table table-md-text-normal table-hover"} )
df = wiki_table[0]#.to_csv('score.csv',index = False)
print(df)
Output:
Rank Address ... Value Analytics
0 1 0xbbda05ea467ad348212dade5c38c11910c14e83e ... $3,054,206.03 NaN
1 2 UniCrypt: Token Vesting ... $2,261,892.00 NaN
2 3 PancakeSwap V2: BIN 17 ... $1,496,520.20 NaN
3 4 Binemon: BIN Token ... $407,340.87 NaN
4 5 0x81da471feb4a45438053dc05e709be056ec26c39 ... $224,172.53 NaN
5 6 0x40bd49e1aa2adaa6595e7372e843ba9483fc4832 ... $127,352.26 NaN
6 7 0xdcdee0f73b9d573de05b93128a84caa7df675956 ... $112,479.07 NaN
7 8 0xd98cad569d4e24e5f1a157cee76a47bdfb026418 ... $107,714.46 NaN
8 9 0xe32b97f5b8ba0d716607e555c8d6099fffb5cdc6 ... $102,848.74 NaN
9 10 0xb1ec3521e766aec113883a984cab330fab4beba5 ... $89,142.96 NaN
10 11 0x0d9b1e53cbb251572d982d9f96520e8d40d22bb0 ... $88,967.75 NaN
11 12 0x18c9cf7f86437398aca31f45365b6168298a5b51 ... $86,177.31 NaN
12 13 0xb96ee668b63c475b86d5b12d3b5bca86d9317f93 ... $85,513.83 NaN
13 14 0xffd2df7ba9a04672933748dc86ce71f131ff4d50 ... $71,639.51 NaN
14 15 0xb6957013a430e4cf509a4c51002073c1b24356e2 ... $69,995.87 NaN
15 16 0xaaa4b610f3c48b427a3ccdc7c18fe70242c30578 ... $69,259.03 NaN
16 17 0x4b0005c7bba3e10820b5b3a2863821e00701b383 ... $68,916.24 NaN
17 18 0x43e5959343cd9154080c235c16fbb4bbd7f83e70 ... $64,840.90 NaN
18 19 0x1349fee2589914fff5460d1998afe09584208b73 ... $61,881.68 NaN
19 20 0xa3da6c54a7451770eb2f7014366490bddeb37d30 ... $61,619.12 NaN
20 21 0x9db651920039b49ba7988848e060d6203e497993 ... $60,642.64 NaN
21 22 0xe02752824b6b11e027080e75f692bd22b3dc7091 ... $59,651.84 NaN
22 23 0x1bb45ab9d1a9af4b7017889ab4f3ef9539d63514 ... $57,265.40 NaN
23 24 0x0eb207b525dc856c3bad5bfd7a7a4aae781e1757 ... $54,586.99 NaN
24 25 0x63ae09700a236fde03b4634fefd39d4bbf8c2332 ... $53,890.98 NaN
25 26 0x8453a1c9eb46769f2620ab05dc6da715ffc5c670 ... $53,815.76 NaN
26 27 0x7c9453d05f91738bfa4175caeaa7bf0b72785670 ... $53,180.22 NaN
27 28 0x0511b95fa08a648bc320c25ae92e65aae807fd4f ... $52,623.27 NaN
28 29 0xd919b251059f8e2262cf6ef90cb8ca602254a9a8 ... $52,563.11 NaN
29 30 0xfd38f35a89b9662c92655f3c4c0366d246116fd6 ... $52,528.32 NaN
30 31 0xd7668f2d5303c4c5202a405c32b1df28051e56ca ... $51,482.65 NaN
31 32 0xbe58dfc5a66fd5ea833f8c6494b5d7e0d68acac5 ... $51,040.69 NaN
32 33 0xa7ae530d3cf06b8245bb38d865a94f3c82d3d041 ... $51,026.66 NaN
33 34 0xd9f12f3e93fe799373f18662d6220860ba46c567 ... $50,478.77 NaN
34 35 0x3ab7822d0c6916953cbde33e1236b329d11e9bb6 ... $47,099.69 NaN
35 36 0x9de94914be7719bd26ae7d33574e154cd6723756 ... $46,120.94 NaN
36 37 0xbdc8e2eb753952013ad07ea248e4445513223e97 ... $46,049.96 NaN
37 38 0x26c7085a67600e85ce0102a8e2ea282e3e56e707 ... $45,884.58 NaN
38 39 0x1fa68bacd8543e0aa7dc95d21f210753c967ef12 ... $45,056.99 NaN
39 40 0x8a6245af6da9447201db7f5611d40ea6e41e7b7c ... $45,020.06 NaN
40 41 0xd7c5d49cf4c4360f6a528cbc5a027c9d7eb632db ... $43,428.15 NaN
41 42 0x40d081b46e08f5c2385cbbdebc31558330ec09f4 ... $42,054.96 NaN
42 43 0x28d18f32c2e175eaed385ec517d1f9ec7bb7b0b0 ... $41,927.94 NaN
43 44 0x59c23a309a3be9ba3a17b2fe3128ed7c947c2990 ... $41,806.24 NaN
44 45 0x790b9a3fe8dc7e59cc1030e0ccc4f415b6c643a2 ... $41,785.23 NaN
45 46 0xd621cf052f5d816043f0def74202bdfcb3401db3 ... $40,096.69 NaN
46 47 0x1ea6ac69ddb06e0eb15b235203bc92673d93c7e5 ... $34,802.68 NaN
47 48 0x9bbfa4001cfbd9f95d3fa25ca2f505f1148649c0 ... $34,042.52 NaN
48 49 0x44b17a9305c565309c32eac1789dfb7e362a65ff ... $33,834.53 NaN
49 50 0x7e9a9d12caa9440ef43b4f2e4b3a42ec3f5f97cd ... $33,477.25 NaN
[50 rows x 6 columns]