如何使用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}")
二、网页爬取技术参考资源
- Requests 标签热门问答:覆盖Requests库的各类使用技巧、请求配置及异常处理
- BeautifulSoup 标签热门问答:包含HTML解析、元素定位、内容提取的实用方案
- Web Scraping 标签热门问答:提供通用爬取思路、反爬策略、数据清洗等进阶内容
内容的提问来源于stack exchange,提问作者Byts
相关产品推荐
相关产品推荐

