javascript - 如何提取两个元素之间的数字?(网页抓取)
问题描述
我从网络抓取开始,我想提取strong
元素之间的数字。
我正在使用 python 3.8 和 beautifulsoup
<li class="price-current">
<span class="price-current-label">
</span>$<strong>409</strong><sup>.99</sup> <a class="price-current-num" href="https://www.newegg.com/gigabyte-radeon-rx-5700-xt-gv-r57xtgaming-oc-8gd/p/N82E16814932208?Item=N82E16814932208&buyingoptions=New">(5 Offers)</a>
<span class="price-current-range">
<abbr title="to">–</abbr>
</span>
</li>
解决方案
要获取 之间的数字<strong>...</strong>
,您可以使用以下示例:
from bs4 import BeautifulSoup
txt = '''<li class="price-current">
<span class="price-current-label">
</span>$<strong>409</strong><sup>.99</sup> <a class="price-current-num" href="https://www.newegg.com/gigabyte-radeon-rx-5700-xt-gv-r57xtgaming-oc-8gd/p/N82E16814932208?Item=N82E16814932208&buyingoptions=New">(5 Offers)</a>
<span class="price-current-range">
<abbr title="to">–</abbr>
</span>
</li>'''
soup = BeautifulSoup(txt, 'html.parser')
print( soup.select_one('.price-current strong').text )
印刷:
409
要获得全部价格(包括价格在内.
),您可以使用re
模块:
import re
price = re.search(r'\$\d+.?\d*', soup.select_one('.price-current').text)
if price:
print(price.group())
印刷:
$409.99
推荐阅读
- python-3.x - Python 3:如何匹配 2 个声波信号
- json - 在 React 中递归渲染深层嵌套数据
- node.js - 赛普拉斯检索 JWToken 并设置为进一步请求的标头
- excel - 如何打开 Web 链接并将文件保存在同一文件夹中?
- android - 如何在android中成功加密和解密tensorflow模型
- html - 单击下拉导航栏时文本移动
- android - wix react-native-navigation 更改选项卡动画
- python - 无法从 Django 视图返回响应到前端
- c# - 如何从动作创建 Observable
? - hibernate - ORA-01400: 当使用 @onetomany 映射并且在子端具有复合主键时,无法将 NULL 插入