【问题标题】:remove html tags from string using bs4使用 bs4 从字符串中删除 html 标签
【发布时间】:2021-06-21 20:08:01
【问题描述】:

我正在尝试制作一个程序来从网站上读取比特币的价格。我使用 bs4 并且很容易获得我正在寻找的部分,但它被 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)

【问题讨论】:

    标签: python html web-scraping beautifulsoup lxml


    【解决方案1】:

    你需要做.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
    

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      相关资源
      最近更新 更多