polukarp
polukarp

Reputation: 54

bs4 How can I extract the text within <p> tag

I'm practicing parsing on https://coinmarketcap.com/currencies/bitcoin/ and I would really like to know, how can I extract the text within this exact <p> tag, as there are lots of those and I want information from only one. Thanks for helping and stuff.

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>

I tried it in different ways, but of many p tags I don't know how to get this exact one.

Upvotes: 2

Views: 161

Answers (2)

baduker
baduker

Reputation: 20052

Use a css selector to grab the paragraph you want.

Here's how:

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())

Output:

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.

Upvotes: 1

Related Questions