使用Python从HTML获取div [英] get div from HTML with Python

查看:697
本文介绍了使用Python从HTML获取div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从HTML页面获取某个div内的值

I want to get a value inside certain div from a HTML page

    <div class="well credit">

      <div class="span2">
          <h3><span>
              $ 5.402 
          </span></h3>
      </div>

    </div>

我已经使用正则表达式(re.seach())完成了该操作,但由于div很大,因此查找div花费的时间太长.

I've done it with regular expressions ( re.seach() ) but it take too long to find the div since it's a huge html.

有没有办法在没有外部库的情况下更快地做到这一点?

Is there a way to do this faster but with no external libraries?

谢谢

推荐答案

我会使用 BeautifulSoup

要获得带有<div>标签的所有内容,只需执行以下操作:

to get everything with <div> tag just do:

soup = BeautifulSoup(html)#make soup that is parse-able by bs
soup.findAll('div') 

要获得跨度范围内的值,您可以执行以下操作:

to get the value inside of span you could do:

soup.find('span').get_text()

有很多获取所需信息的不同方法

there are tons of differnt methods of getting the informaton you need

祝您好运,希望对您有帮助!

Good Luck hope this helps!

这篇关于使用Python从HTML获取div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆