正则表达式搜索产品名称并获得价格

时间:2017-05-26 12:25:10

标签: c# regex web-scraping web-crawler screen-scraping

我有产品名称列表,我想在以下给定的URL中搜索产品名称,我只需要获取价格。 例如,我想搜索“Tommee Tippee一次性乳垫 - 1 x 50包” 在这个URL上 http://www.boots.ie/baby-child/babyfeeding/breastfeeding-pumps 如果我成功匹配那么我怎么能得到价格,即€8.49 通过刮擦。 这是一个演示网址,类似我有网址列表

请帮我提供任何示例或正则表达式来完成这项工作

1 个答案:

答案 0 :(得分:0)

一些示例代码可能会帮助您启动此项目。

import re
from bs4 import BeautifulSoup

url = 'http://www.boots.ie/baby-child/babyfeeding/breastfeeding-pumps'
soup = BeautifulSoup(url, 'html.parser')
product_name_regex = 'Tommee Tippee Disposable Breast Pads - 1 x 50 Pack'
product_tag = soup.find('div', text=re.compile(product_name_regex))
price_tag = product_tag.find_next('div', { "class" : "product_price" })
price = price_tag.text

这个简短的代码试图首先解析html页面,然后通过使用文本/类名称查找标记从该页面中提取信息。

一些可能对您有帮助的链接: BeautifulSoup DocHow to find elements by class

相关问题