深色proxyscrape 徽标

Web Scraping for Price Comparison in 2024– Easy Steps

蟒蛇, 搜索, Jan-03-20235 分钟阅读

网络搜索是从互联网中提取数据的艺术。其应用领域非常广泛。不同网站的价格比较就是其中之一。现在,网上购物已成为行业的热潮,对某些产品的价格进行比较已成为一种必需。当我们需要购买某一特定产品时,我们都会访问多个网站,但您是否想过制作一个价格比较工具,为您完成同样的工作,并将最优惠的价格放在您面前?  

在本文中,我们将用 Python 制作一个神奇的网络搜刮比价工具,它可以让你跟踪不同来源的产品价格,并告知你不同竞争对手的市场表现。此外,它还会告知企业特定产品的价格是否会比预测价格上涨或下降。

本文使用的数据源是一个 JSON 文件,我们将比较从亚马逊、eBay 和沃尔玛获得的产品价格。我们的示例数据如下、

欢迎跳转到任何章节,了解更多有关用 python 进行价格比较的网络刮擦的信息!

目录

使用网络搜索进行价格比较的步骤:

[
  {
    "last_visited": "2018-01-30T13:38:01",
    "name": "PUMA Men's Evospeed 17.4 TT Soccer Shoe",
    "amazon_price": 36.94,
    "ebay_price": 37,
    "walmart_price": 37,
    "amazon_url": "https://www.amazon.com/PUMA-Evospeed-Soccer-Ultra-Yellow-Peacoat-Orange/dp/B01J5LEMZI/",
    "ebay_url": "https://www.ebay.com/itm/PUMA-Mens-Evospeed-17-4-Tt-Soccer-Shoe/302471489090",
    "walmart_url": "https://www.walmart.com/ip/PUMA-Men-s-Evospeed-17-4-Tt-Soccer-Shoe/587074448",
    "description": "The new evospeed 17.4 is a performance football boot for players of all levels. The soft and lightweight synthetic leather on the upper keeps the boot lightweight, comfortable and ensures durability. The lightweight outsole offers the perfect balance between traction, stability and acceleration PUMA is the global athletic brand that successfully fuses influences from sport, lifestyle and fashion. PUMA's unique industry perspective delivers the unexpected in sport-lifestyle footwear, apparel and accessories, through technical innovation and revolutionary design.",
    "brand": "PUMA",
    "image": "https://images-na.ssl-images-amazon.com/images/I/61v1mylcAqL._UL1500_.jpg"
  },
  {
    "last_visited": "2018-01-30T13:38:07",
    "name": "L'Oreal Paris Skin Care Revitalift Cicacream Face Moisturizer",
    "amazon_price": 13.97,
    "ebay_price": 13.99,
    "walmart_price": 13.97,
    "amazon_url": "https://www.amazon.com/LOreal-Paris-Revitalift-Cicacream-Moisturizer/dp/B074MBDRHW",
    "ebay_url": "https://www.ebay.com/itm/LOREAL-Paris-NEW-Revitalift-Cicacream-Anti-Wrinkle-Skin-Barrier-Repair-ORIGINAL/112715734801",
    "walmart_url": "https://www.walmart.com/ip/L-Or-al-Paris-Revitalift-Cicacream-Anti-Wrinkle-Skin-Barrier-Repair/519350834",
    "description": "Skin's moisture barrier weakens with age, resulting in greater moisture loss, more prominent wrinkles and loss of firmness. Lightweight, protective cream is formulated with Pro-Retinol, a powerful wrinkle-fighting ingredient and Centella Asiatica, an herb used in traditional Chinese medicine. Strengthens and repairs skin barrier to help resist visible lines, loss of firmness and other signs of aging that a weakened skin barrier can accentuate. See visible results immediately: skin feels healthier, softer, smoother and more supple. Skin feels noticeably more hydrated. Skin barrier is stronger, helping to resist signs of aging. In two weeks: fine lines appear visibly reduced. Firmness and elasticity look noticeably improved. In four weeks: wrinkles appear less visible. Clarity and tone improves, skin exudes luminosity. Skin continues to look and feel soft, smooth, healthy.",
    "brand": "L'Oreal Paris",
    "image": "https://images-na.ssl-images-amazon.com/images/I/71Ff2vn4vjL._SL1500_.jpg"
  },
  {
    "last_visited": "2018-01-30T13:38:12",
    "name": "Adidas Dynamic Pulse By Adidas For Men",
    "amazon_price": 6.96,
    "ebay_price": 18.99,
    "walmart_price": 7,
    "amazon_url": "https://www.amazon.com/Adidas-Dynamic-Toilette-3-4-Ounce-Bottle/dp/B000VON5F2/",
    "ebay_url": "https://www.ebay.com/itm/Adidas-DYNAMIC-PULSE-Cologne-for-Men-3-4-oz-edt-3-3-Spray-New-in-BOX/252837623533",
    "walmart_url": "https://www.walmart.com/ip/Adidas-Dynamic-Pulse-for-Men-3-4-oz-EDT/28664356",
    "description": "Launched by the design house of Adidas in 1997, ADIDAS DYNAMIC PULSE is a men's fragrance that possesses a blend of A fresh scent of citrus, cedar and mint with low tones of sweet fruits, fragrant woods and tonka bean. It is recommended for daytime wear.When applying any fragrance please consider that there are several factors which can affect the natural smell of your skin and, in turn, the way a scent smells on you. For instance, your mood, stress level, age, body chemistry, diet, and current medications may all alter the scents you wear. Similarly, factor such as dry or oily skin can even affect the amount of time a fragrance will last after being applied",
    "brand": "adidas",
    "image": "https://images-na.ssl-images-amazon.com/images/I/41%2BAnOP5nbL.jpg"
  },
  {
    "last_visited": "2018-01-30T13:38:19",
    "name": "Canon EOS Rebel T6 Digital SLR Camera",
    "amazon_price": 449,
    "ebay_price": 449,
    "walmart_price": 449,
    "amazon_url": "https://www.amazon.com/Canon-Digital-Camera-18-55mm-3-5-5-6/dp/B01CO2JPYS",
    "ebay_url": "https://www.ebay.com/itm/Canon-EOS-Rebel-T6-DSLR-Camera-with-18-55mm-Lens/232596041502",
    "walmart_url": "https://www.walmart.com/ip/Canon-EOS-Rebel-T6-DSLR-Camera-with-18-55mm-Lens-Black/50820749",
    "description": "",
    "brand": "Canon",
    "image": "https://images-na.ssl-images-amazon.com/images/I/81YszfZS8%2BL._SL1500_.jpg"
  },
  {
    "last_visited": "2018-01-30T13:38:25",
    "name": "Woodland Fox Critter 36' Mylar Balloon",
    "amazon_price": 5.49,
    "ebay_price": 6.49,
    "walmart_price": 7.6,
    "amazon_url": "https://www.amazon.com/Woodland-Fox-Critter-Mylar-Balloon/dp/B00S9TKVYO",
    "ebay_url": "https://www.ebay.com/itm/Woodland-Critters-Fox-36-inch-Foil-Balloon/132058119680",
    "walmart_url": "https://www.walmart.com/ip/Woodland-Fox-Foil-Balloon/43350002",
    "description": "Celebrate any occasion with an adorable woodland fox critter balloon! 36\" Woodland Critters fox shape foil balloon.",
    "brand": "Betallic",
    "image": "https://images-na.ssl-images-amazon.com/images/I/71Z9bG-BzuL._SL1500_.jpg"
  }
]

与我们正在编写的脚本相关的一些重要字段是amazon_priceebay_price 和walmart_price。

现在我们已经看到了我们的数据。让我们进入开发阶段。

我们将使用 Python 3.x 制作该工具,首先,我们将使用 JSON 库解析 JSON并进行进一步处理。该工具通过打印网站的产品名称和价格提供了惊人的功能。我们将导入 JSON 库来解析 JSON。

导入json

现在,我们将调用代码片段中的 open() 函数,从 JSON 文件中读取内容、

导入json
 
if__name__ =='__main__':
    price_data =None
    price = []
   with open('data.json', encoding='utf8')asf:
        price_data = f.read()
 
   如果price_data不是 None:
       json_price_data = json.loads(price_data)

现在 JSON 数据已读取,我们将把它转换成 Python 内置的数据结构,为此代码将调用 json.loads() 方法,根据条目将 JSON 字符串转换成字典或字典列表。

由于我们的主要目标是找到以最低价格销售产品的商店,因此我们的目标是找到最低价格和其他相关详细信息,如产品和商店名称。相关商店的价格信息存储在amazon_price、ebay_ price 和 Walmart_price 密钥中。要找到每个产品的最低价,我们需要遍历价格列表项。

for d in json_price_data:
            price.append({'name': d['name'], 'price': float(d['amazon_price']), 'url': d['amazon_url']})
            price.append({'name': d['name'], 'price': float(d['walmart_price']), 'url': d['walmart_url']})
            price.append({'name': d['name'], 'price': float(d['ebay_price']), 'url': d['ebay_url']})
            minPricedItem = min(price, key=lambda x: x['price'])
            print(minPricedItem)
            print('=================')
            price = []

我们使用 lambdas 并设置 min() 的关键字,以确保价格字段被比较。结果如下

让我们稍微调整一下格式。

for d in json_price_data:
            price.append({'name': d['name'], 'price': d['amazon_price'], 'url': d['amazon_url']})
            price.append({'name': d['name'], 'price': d['walmart_price'], 'url': d['walmart_url']})
            price.append({'name': d['name'], 'price': d['ebay_price'], 'url': d['ebay_url']})
            minPricedItem = min(price, key=lambda x: float(x['price']))
            store_name = ''
            # Pick the store name based on url
            if 'amazon' in minPricedItem['url'].lower():
                store_name = 'Amazon'
            elif 'walmart' in minPricedItem['url'].lower():
                store_name = 'Amazon'
            elif 'ebay' in minPricedItem['url'].lower():
                store_name = 'eBay'
            print('{} is available in cheap price at {}. The price is ${}'.format(minPricedItem['name'], store_name,
                                                                                 minPricedItem['price']))
            price = []

输出结果如下

恭喜您!我们已经成功制作了脚本,您可以定期运行脚本来获取产品的最新价格。

使用 Python 进行价格比较时,哪种网络抓取代理最好?

ProxyScrape 是网上最受欢迎、最可靠的代理服务提供商之一。三种代理服务包括专用数据中心代理服务器、住宅代理服务器和高级代理服务器。那么,使用 python 进行价格比较的最佳 HTTP 代理服务器的最佳解决方案是什么?在回答这个问题之前,最好先看看每个代理服务器的特点。

专用数据中心代理 最适用于高速在线任务,如从不同服务器流式传输大量数据(就大小而言)以进行分析。这也是企业选择专用代理在短时间内传输大量数据的主要原因之一。

专用数据中心代理具有多种功能,如无限带宽和并发连接、便于通信的专用 HTTP 代理以及更安全的 IP 验证。专用数据中心的正常运行时间为 99.9%,在任何会话期间都能正常工作,您大可放心。最后但同样重要的是,ProxyScrape 提供优质的客户服务,将在 24-48 个工作小时内帮助您解决问题。 

其次是 住宅代理。 住宅代理是每个普通消费者的首选代理。主要原因是住宅代理的 IP 地址与 ISP 提供的 IP 地址相似。这意味着从目标服务器获得访问其数据的许可会比平常更容易。 

ProxyScrape住宅代理的另一个特点是轮换功能。旋转代理可以帮助您避免账户被永久封禁,因为住宅代理会动态更改您的 IP 地址,使目标服务器难以检查您是否在使用代理。 

除此之外,住宅代理的其他功能还包括:无限带宽、并发连接、专用 HTTP/s 代理、随时会话代理(因为代理池中有 700 多万个代理)、用户名和密码验证以提高安全性,最后但并非最不重要的一点是能够更改国家服务器。您可以通过在用户名验证中附加国家代码来选择所需的服务器。 

最后一个是 高级代理。高级代理与专用数据中心代理相同。功能保持不变。主要区别在于可访问性。在高级代理中,代理列表(包含代理的列表)向ProxyScrape网络上的每个用户开放。这就是为什么高级代理的成本低于专用数据中心代理的原因。

那么,对于使用 python 进行价格比较的网络搜刮而言,最佳 HTTP 代理的最佳解决方案是什么?答案是"住宅代理."原因很简单。如上所述,住宅代理是一种旋转代理,这意味着您的 IP 地址会在一段时间内动态变化,这有助于在一小段时间内发送大量请求,从而欺骗服务器,而不会被屏蔽 IP。 

接下来,最好是根据国家更改代理服务器。您只需在 IP 验证或用户名和密码验证的末尾添加国家 ISO_CODE。 

推荐阅读

  1. 抓取 YouTube 评论 - 5 个简单步骤
  2. 2023 年八大最佳 Python 网络抓取工具
  3. 使用 Python 进行新闻文章网络抓取--2023 年的最佳方法

常见问题:

1.什么是削价?
价格搜索,顾名思义,就是在线提取产品或服务的价格,以进行任何分析,如竞争对手分析,从而改进营销策略。将价格搜索过程自动化可以帮助你减少时间和资源,而你可以在 python 的帮助下做到这一点。
2.什么是用于价格比较的网络搜索的最佳代理?
The best proxy to perform web scraping for price comparison is a “residential proxy.” The reason is that the residential proxy is a rotating proxy, meaning that your IP address would be dynamically changed over a period of time which can be helpful to trick the server by sending a lot of requests within a small time frame without getting an IP block.
3.用于比价的网络搜索是否合法?
答案是肯定的。您可以从电子商务网站上抓取价格,因为所有信息都是公开的,这意味着所有公开数据都可以被抓取。

结论

本文探讨了网络搜刮的另一个神奇之处,即 "价格比较"。不仅如此,我们还建立了一个工具,可以为你完成价格比较工作,并让你随时了解市场趋势。本文希望通过简单的方式,为大家提供足够的有关网络搜刮比价的信息。代理服务器是网络搜刮的最佳伴侣。ProxyScrape 为您的网络搜刮比价项目提供同类最佳的住宅代理服务器。您可以在此查看最佳住宅代理。