【问题标题】:Beautiful Soup: can't get the text (price) out of this [duplicate]Beautiful Soup:无法从中获取文本(价格)[重复]
【发布时间】:2020-04-02 20:12:34
【问题描述】:

[ 我怎样才能从这个烂摊子中得到价格。]

<li class="price-current">
               <span class="price-current-label">
               </span>₹ 36,659 <a class="price-current-num" href="https://www.newegg.com/global/in-en/gigabyte-geforce-rtx-2070-super-gv-n207swf3oc-8gd/p/N82E16814932213?Item=N82E16814932213&amp;buyingoptions=New">(3 Offers)</a>
               <span class="price-current-range">
               <abbr title="to">–</abbr>
               </span>
 </li>

【问题讨论】:

  • 请不要将 HTML 代码作为图片发布。编辑您的问题并将其作为文本放在那里。这样我们就可以复制它并尝试找到解决方案。
  • 好的!感谢您的建议。
  • 如果您在浏览器中搜索“Python 从 HTML 获取字段”,您会找到比我们在此处管理的更能解释这一点的参考资料。我们希望您在此处发布之前进行基本搜索。

标签: python html python-3.x beautifulsoup


【解决方案1】:

您可以使用.find_next_sibling()text=True 参数来定位价格:

data = '''<li class="price-current">
               <span class="price-current-label">
               </span>₹ 36,659 <a class="price-current-num" href="https://www.newegg.com/global/in-en/gigabyte-geforce-rtx-2070-super-gv-n207swf3oc-8gd/p/N82E16814932213?Item=N82E16814932213&amp;buyingoptions=New">(3 Offers)</a>
               <span class="price-current-range">
               <abbr title="to">–</abbr>
               </span>
 </li>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print(soup.select_one('.price-current-label').find_next_sibling(text=True))

打印:

₹ 36,659 

【讨论】:

  • 非常感谢。我真的很感激。
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
相关资源
最近更新 更多