python - 怎么从BeautifulSoup得到的ResultSet里搜索想要的部分?
黄舟
黄舟 2017-04-17 17:09:55
0
6
1071

python脚本里的语句是

from bs4 import BeautifulSoup import urllib2 response = urllib2.urlopen('http://xxxxxx') html = response.read() soup = BeautifulSoup(html) res = soup.find_all(class_='pro-detail-price')

得到的res是ResultSet类型,print结果是:

[

最低价格:4399.0(天猫旗舰店)




]

如果想在这个ResultSet里找到特定的标签改用什么办法呢? 比如找到4399.0里的4399.0

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全員に返信 (6)
洪涛
res = soup.find(class_='pro-detail-price').find('span').string

这么可以吗?

いいねを押す+0
    巴扎黑

    我记得find只会返回第一个匹配到的结果,findAll才会返回ResultSet
    所以find结果应该可以直接 res.span.string啊,实在不行就都res[0].span.string吧

    いいねを押す+0
      大家讲道理

      find_all()find()都不好用,bs最好用的是select(),即CSS选择器。
      你已经获取了一个大框架吧,比如它是soup中的一个Tag,然后:

      results = tag.select('p[class="pro-detail-price"] span')

      你这个例子里,results[0]就是这个指定的标签了。

      いいねを押す+0
        大家讲道理
        from bs4 import BeautifulSoup import urllib2 response = urllib2.urlopen('http://xxxxxx') html = response.read() soup = BeautifulSoup(html) res = soup(class_='pro-detail-price') for sp in res: print sp.span.string

        胡乱试了一下,改成这样的代码输出结果是4399.0,但是希望大神能提供更优的方式,谢谢

        いいねを押す+0
          刘奇

          在find找到的结果里,可以用CSS选择器select进行下一的了定位查找。

          いいねを押す+0
            刘奇
            soup.find("p", class_='pro-detail-price').find("span").text
            いいねを押す+0
              最新のダウンロード
              詳細>
              ウェブエフェクト
              公式サイト
              サイト素材
              フロントエンドテンプレート
              私たちについて 免責事項 Sitemap
              PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!