【问题标题】:python search for tag using bs4python使用bs4搜索标签
【发布时间】:2019-02-08 09:46:19
【问题描述】:
import urllib.request
import bs4 

url = ""
html = urllib.request.urlopen(url).read()
soup = bs4.BeautifulSoup(html, 'html.parser')

我正在尝试使用 python 3 将网站中的值 15.720 保存到变量中。目前我已将 html 保存到汤变量中。

如何搜索下面的行并将数字 15.720 保存到变量中?

<td class="myclass" rowspan="2">lor be: <strong>15.720</strong></td>

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案1】:

    使用soup.find("td", class_="myclass").strong.text

    例如:

    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup('<td class="myclass" rowspan="2">lor be: <strong>15.720</strong></td>', "html.parser")
    print(soup.find("td", class_="myclass").strong.text)
    

    输出:

    15.720
    

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多