使用bs4从字符串中删除html标记

2024-06-10 03:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图制作一个程序,从网站上读取比特币的价格。我使用bs4和bale来获取我要查找的部分,但它被html标记包围

   output: <div class="priceValue___11gHJ">$52,693.18</div>  

我只想知道价格,我已经尝试过regex和lxml方法,但是我不断地出错

import requests
from bs4 import BeautifulSoup

#get url
url = "https://coinmarketcap.com/currencies/bitcoin/"
r = requests.get(url)

#parse html
soup = BeautifulSoup(r.content, 'html5lib')

#find div
find_div = soup.find('div', {"class": "priceValue___11gHJ"})
print(find_div)

Tags: import程序divurlgethtml价格find
1条回答
网友
1楼 · 发布于 2024-06-10 03:38:17

您需要执行.text

import requests
from bs4 import BeautifulSoup

#get url
url = "https://coinmarketcap.com/currencies/bitcoin/"
r = requests.get(url)

#parse html
soup = BeautifulSoup(r.content, 'html5lib')

#find div
find_div = soup.find('div', {"class": "priceValue___11gHJ"})
print(find_div.text) # $52,693.18

相关问题 更多 >