You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python Requests与BS4提取WooCommerce价格?求爬取技术参考

提取HTML中的数值及网页爬取参考资源

一、使用Requests和BeautifulSoup提取目标数值

针对给定HTML片段的处理代码

from bs4 import BeautifulSoup

# 给定的HTML代码
html = '<p>[₱81,495.00]</p>'

# 解析HTML
soup = BeautifulSoup(html, 'html.parser')

# 获取p标签文本并清理内容
raw_text = soup.find('p').get_text(strip=True)
target_value = raw_text.strip('[]').replace('₱', '').strip()

print(target_value)  # 输出:81,495.00

从实际网页爬取的完整代码

如果需要从在线网页获取该内容,可结合Requests库:

import requests
from bs4 import BeautifulSoup

# 替换为目标网页的URL
target_url = "https://example.com/target-page"

try:
    # 发送请求获取网页内容
    response = requests.get(target_url)
    response.raise_for_status()  # 捕获HTTP请求错误

    # 解析网页
    soup = BeautifulSoup(response.text, 'html.parser')

    # 定位目标p标签(若页面有多个p标签,需根据class/id等属性精准定位)
    price_tag = soup.find('p')
    if price_tag:
        raw_text = price_tag.get_text(strip=True)
        target_value = raw_text.strip('[]').replace('₱', '').strip()
        print(f"提取到的数值:{target_value}")
    else:
        print("未找到包含目标数值的p标签")
except requests.exceptions.RequestException as e:
    print(f"请求出错:{e}")

二、网页爬取技术参考资源

内容的提问来源于stack exchange,提问作者Byts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:07:53