无法解析beautifulsoup上的跨度ID [英] Can't parse span id on beautifulsoup

查看:19
本文介绍了无法解析beautifulsoup上的跨度ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个刮板,但我遇到了一个问题.我可以解析跨度类"和div中的类"但是当我尝试解析跨度中的 id"时;它不会打印我想要的数据.

i am trying to write a scraper but i have faced with an issue. I can parse "class in spans" and "class in div" but when i try to parse "id in span" it doesn't print the data i want.

from bs4 import BeautifulSoup
from urllib import request
from urllib.request import Request, urlopen

req = Request('https://bscscan.com/token/0xc3d33bdd0b6cea10eb496fbc7592e45f2624c0a5', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = BeautifulSoup(webpage, 'html.parser')


name = soup.find('span', class_='text-secondary small').text
add = soup.find('div', class_='mr-3').text
trans = soup.find('span', attrs={'id':'totaltxns'}).text



print(name, add, trans)

推荐答案

您需要获取会话 cookie,然后向其他端点发出请求.sid 也需要动态获取.

You need to pick up a session cookie then make a request to an additional endpoint. sid needs to be dynamically picked up as well.

import requests, re

def get_transfer_count(str:token)->str:
    
    with requests.Session() as s:
        s.headers = {'User-Agent':'Mozilla/5.0'}
        r = s.get(f'https://bscscan.com/token/{token}')
        sid = re.search(r"var sid = '(.*?)'", r.text).group(1)
        r = s.get(f'https://bscscan.com/token/generic-tokentxns2?m=normal&contractAddress={token}&a=&sid={sid}&p=1')
        return re.search(r"var totaltxns = '(.*?)'", r.text).group(1)
    
token = '0x8df9655178350146eAD837B15ba3D32c9Fe1497d'

get_transfer_count(token)

这篇关于无法解析beautifulsoup上的跨度ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆