gpt4 book ai didi

python - bs4 如何提取

标签内的文本

转载 作者:行者123 更新时间:2023-12-04 08:05:31 25 4
gpt4 key购买 nike

我正在练习解析 https://coinmarketcap.com/currencies/bitcoin/我真的很想知道,我怎样才能提取此精确中的文本 <p>标签 ,因为有很多这样的信息,而我只想要一个信息。感谢您的帮助和东西。

import requests as r
from bs4 import BeautifulSoup

def find_info(self):
api = r.get(self.url) #url is above in the description
soup = BeautifulSoup(api.text, "html.parser")
soup.find_all('p')

# and here I'm stuck.
# I need to get the text from the chunk of HTML below.

<p>
<strong>
Bitcoin price today
</strong>
is ₽3.795.164 RUB with a 24-hour trading volume of ₽6.527.780.409.893 RUB. Bitcoin is down,12% in the last 24 hours. The current CoinMarketCap ranking is #1, with a market cap of ₽70.707.857.530.563 RUB. It has a circulating supply of 18.631.043 BTC coins and a max. supply of 21.000.000 BTC coins.
</p>
我以不同的方式尝试过,但是在许多 p 标签中,我不知道如何获得这个确切的标签。

最佳答案

使用 css selector捕获你想要的段落。
就是这样:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://coinmarketcap.com/currencies/bitcoin/").content
print(BeautifulSoup(page, "html.parser").select_one('.about___1OuKY p').getText())
输出:
Bitcoin price today is $51,393.64 USD with a 24-hour trading volume of $88,784,693,272 USD. Bitcoin is up 4.87% in the last 24 hours. The current CoinMarketCap ranking is #1, with a market cap of $957,517,202,639 USD. It has a circulating supply of 18,631,043 BTC coins and a max. supply of 21,000,000 BTC coins.

关于python - bs4 如何提取 <p> 标签内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66241187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com