简体   繁体   English

获取 python 中 2 个 div 标签之间的文本

[英]get text between 2 div tags in python

I'm making a script that gets bitcoin prices from multiple sites, and on one of the sites the text is between 2 div tags.我正在制作一个从多个站点获取比特币价格的脚本,在其中一个站点上,文本位于 2 个 div 标签之间。

I tried multiple solutions but nothing worked, however I found the same issue here and this is exactly what I need but in python我尝试了多种解决方案,但没有任何效果,但是我在这里发现了同样的问题,这正是我需要的,但在 python

Note: the text is updating every 2 seconds on the site注意:文本在网站上每 2 秒更新一次

this is what i need to get这就是我需要得到的

<div data-bn-type="text" class="css-g80xfv" style="direction: ltr;">$19,490.20</div>
                                                                   ^^^^^^^^^^^

site - https://www.binance.com/en网站 - https://www.binance.com/en

Xpath - //*[@id="__APP"]/div[2]/main/div/div[4]/div/a[2]/div[2]/div Xpath - //*[@id="__APP"]/div[2]/main/div/div[4]/div/a[2]/div[2]/div

I know there's an API on this site but I just want to get the price nothing else我知道这个网站上有一个 API 但我只想得到价格

thanks谢谢

I think you may need to use selenium since the actual price information on that page is loaded dynamically, here is an example that gets an update every 2 seconds:我认为您可能需要使用selenium因为该页面上的实际价格信息是动态加载的,这是一个每2秒更新一次的示例:

import time
from bs4 import BeautifulSoup
from decimal import Decimal
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

BASE_URL = 'https://www.binance.com/en'
BITCOIN_HREF = '/en/trade/BTC_BUSD'
LAST_PRICE_COLUMN_CLASS = 'css-g80xfv'
WAIT_SECONDS = 2

def main():
    driver = webdriver.Chrome(ChromeDriverManager().install())

    for _ in range(5): # replace with `while True:` if you want to get updates "indefinitely"
        driver.get(BASE_URL)

        time.sleep(WAIT_SECONDS)

        html = driver.page_source
        soup = BeautifulSoup(html, 'html.parser')
    
        btc_a_tag = soup.find('a', href=BITCOIN_HREF)
        btc_price_string = btc_a_tag.find('div', class_=LAST_PRICE_COLUMN_CLASS).text
        btc_price_decimal = Decimal(btc_price_string.strip('$').replace(',', ''))
        print(f"btc_price_string={btc_price_string}, btc_price_decimal={btc_price_decimal}")

    driver.close()

if __name__ == '__main__':
    main()

Example Output:示例 Output:

btc_price_string=$19,306.29, btc_price_decimal=19306.29
btc_price_string=$19,307.85, btc_price_decimal=19307.85
btc_price_string=$19,308.18, btc_price_decimal=19308.18
btc_price_string=$19,308.41, btc_price_decimal=19308.41
btc_price_string=$19,308.18, btc_price_decimal=19308.18

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM