【问题标题】:How to grab the percentage values inside the table in python and beautifulsoup如何在python和beautifulsoup中获取表格内的百分比值
【发布时间】:2021-10-27 06:49:09
【问题描述】:

感谢@Ram,我已经设法找出我正在尝试工作的 url 的 iframe 直接链接。但是我的新 sn-p 有一些问题。

问题 #1:不显示正确的百分比值

问题 #2:我不知道如何在“i class="far fa-file-alt text-secondary" data-toggle="tooltip" title="" data-original 中获取标题文本“Contract” -title="合同"

import requests
from bs4 import BeautifulSoup

url = "https://bscscan.com/token/generic-tokenholders2?m=normal&a=0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3"
header = {"User-Agent": "Mozilla/5.0"}

reqblockdetails = requests.get(url,header, timeout=5)
soupblockdetails = BeautifulSoup(reqblockdetails.content, 'html.parser')
rowsblockdetails = soupblockdetails.findAll('table')[0].findAll('tr', limit=10)#[1:]

for row in rowsblockdetails[1:]:
    rank = row.find_all('td')[0].text[0:].strip()
    address = row.find_all('td')[1].text[0:].strip()
    amount = row.find_all('td')[2].text[0:].strip()
    percentage = row.find_all('td')[3].text[0:]
  
    print (" {:<3} {:<25}  {:>15} {:>10} ".format(rank, address, amount, percentage))

Current Output: # 最后一列显示 0.0000% 是问题

 1   0x0000000000000000000000000000000000000001  422,619,084,856,497.90460343   0.0000%
 2   0x8c128dba2cb66399341aa877315be1054be75da8  37,995,040,112,894.283191536   0.0000%  
 3   0xa8736b9585a01d6dcc1b6e2fc9dc208552c34b58  20,000,000,001,566.132214712   0.0000%  
 4   PancakeSwap: SAFEMOON                       14,559,745,452,918.021161369   0.0000%  
 5   0xff3dd404afba451328de089424c74685bf0a43c9  14,301,351,821,601.347718476   0.0000%  

想要的输出:#-- 合同来自 -> i class="far fa-file-alt text-secondary"

 1   0x0000000000000000000000000000000000000001  422,619,084,856,497.90460343   42.2608%  
 2   0x8c128dba2cb66399341aa877315be1054be75da8  37,995,040,112,894.283191536    3.7991%  
 3   0xa8736b9585a01d6dcc1b6e2fc9dc208552c34b58  20,000,000,001,566.132214712    2.0000%     Contract 
 4   PancakeSwap: SAFEMOON                       14,559,745,452,918.021161369    1.4684%     Contract
 5   0xff3dd404afba451328de089424c74685bf0a43c9  14,301,351,821,601.347718476    1.4421%     Contract

【问题讨论】:

  • 来自 URL 本身,显示为 0.0000%
  • 我现在做了检查,你的权利。谢谢你。我将改为编辑我的问题。

标签: python python-3.x beautifulsoup request


【解决方案1】:
for row in rowsblockdetails[1:]:
    rank = row.find_all('td')[0].text[0:].strip()
    address = row.find_all('td')[1].text[0:].strip()
    amount = row.find_all('td')[2].text[0:].strip()
    percentage = row.find_all('td')[3].text[0:]

    try:
        cnt=row.select_one("i.far.fa-file-alt.text-secondary")['title']
    except:
        cnt=""
  
    print (" {:<3} {:<25}  {:>15} {:>10} {:<20} ".format(rank, address, amount, percentage,cnt))

输出:

 1   0x0000000000000000000000000000000000000001  422,620,418,588,552.378931106   0.0000%                       
 2   0x8c128dba2cb66399341aa877315be1054be75da8  37,990,958,290,743.368378822   0.0000%                       
 3   0xa8736b9585a01d6dcc1b6e2fc9dc208552c34b58  20,000,000,001,566.132214712   0.0000%  Contract 
.....

【讨论】:

  • 谢谢。我设法编辑了网址。它现在正在您的想法和帮助下工作。
猜你喜欢
  • 2020-09-15
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
相关资源
最近更新 更多