使用BeautifulSoup4的Python Web抓取标签用于英镑价格

时间:2015-12-27 17:24:45

标签: python html regex web-scraping

首先发帖所以请温柔。我正在尝试使用Python进行网络搜索。

我的目标是制作一个可以访问以下页面的脚本:

“WWWzaraDOTcom / UK / EN /销售/女人/礼服/视图全c731533.html”

我希望脚本返回a.name.item,“data-ecirp”中的数据,其中span类分别为“sale”和“crossOut diagonal-line”。

我想及时在其他网站上使用此脚本。

我下面的当前代码可以访问Zara,拉页面并在RE / BS中操作 - 但是在使用BeautifulSoup和span标签进行大量Google搜索后,我无法提取任何价格。

正则表达式伤害了我的大脑,我无法从我的书中获得任何简单的代码示例来工作IRL。

  • 是教导BeautifulSoup正确遍历的答案
  • 或者使用RE来推断设置字符串中的值?
  • 还是我应该采取另一种更好的方法。 (我想在事情最终出售时提醒......)

    import requests
    import re
    
    r =      requests.get(“http://www.zara.com/uk/en/sale/woman/dresses/view-all-c731533.html”)
    r.raise_for_status() 
    html = r.text
    soup = BeautifulSoup(html, 'html.parser')
    

1 个答案:

答案 0 :(得分:4)

您想要的所有数据都在product-info标记中,网址也不同,因为您必须首先选择一个地区:

import requests

from bs4 import BeautifulSoup
r =   requests.get("http://www.zara.com/ie/en/sale/woman/dresses/view-all-c731533.html?#utm_referrer=http%3A%2F%2Fwww.zara.com%2F%3Fgo%3Dhttp%253A%2F%2Fwww.zara.com%2Fshare%2Fsale%2Fwoman%2Fdresses%2Fview-all-c731533.html")
r.raise_for_status()
html = r.text
soup = BeautifulSoup(html)
prods = soup.find_all("div", {"class": "product-info"})
print(prods)
for p in prods:
    print(p.a.text)
    print("Old price  {}".format(p.find("span", {"class": "crossOut"})["data-ecirp"]))
    print("New price {}".format(p.find("span", {"class": "sale"})["data-ecirp"]))

输出:

ZIP-BACK TUBE DRESS
Old price  49.95  EUR
New price 29.99  EUR
SEQUINNED DRESS
Old price  69.95  EUR
New price 29.99  EUR
HALTER NECK SHIMMER THREAD DRESS
Old price  39.95  EUR
New price 25.99  EUR
CREPE SLEEVELESS DRESS
Old price  49.95  EUR
New price 29.99  EUR
OTTOMAN RIB DRESS
Old price  39.95  EUR
New price 25.99  EUR
SHORT LACE DRESS
Old price  49.95  EUR
New price 39.99  EUR
SPARKLY VELVET DRESS
Old price  49.95  EUR
New price 29.99  EUR
DRESS WITH SIDE GATHERING
Old price  19.95  EUR
New price 12.99  EUR
TUBE DRESS WITH STRAPPY BACK
Old price  59.95  EUR
New price 49.99  EUR
SKATER DRESS
Old price  49.95  EUR
New price 39.99  EUR
PARTY DRESS
Old price  49.95  EUR
New price 29.99  EUR
DOUBLE LAYER DRESS
........................

将数据存储在dict中可能是一个好主意:

from collections import defaultdict
d = defaultdict(defaultdict)
for p in prods:
    k = p.a.text
    v1 = p.find("span", {"class": "crossOut"})["data-ecirp"]
    v2 = p.find("span", {"class": "sale"})["data-ecirp"]
    d[k]["new_price"] = v1
    d[k]["old_price"] = v2
    d[k]["saving"] = float(v1.split()[0]) - float(v2.split()[0])

print(d)

哪个会给你:

defaultdict(<class 'collections.defaultdict'>, {'': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}),
 'CROCHET DRESS': defaultdict(None, {'new_price': '49.95  EUR','saving': 19.960000000000004, 'old_price': '29.99  EUR'}),
 'BASIC SLEEVELESS DRESS': defaultdict(None, {'new_price': '14.95  EUR', 'saving': 4.959999999999999, 'old_price': '9.99  EUR'}), 'FLORAL PRINT TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'COMBINED DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'SEQUINNED DRESS': defaultdict(None, {'new_price': '69.95  EUR', 'saving': 39.96000000000001, 'old_price': '29.99  EUR'}), 'LONG CROCHET DRESS': defaultdict(None, {'new_price': '89.95  EUR', 'saving': 39.96, 'old_price': '49.99  EUR'}), 'STRIPED DRESS': defaultdict(None, {'new_price': '19.95  EUR', 'saving': 6.959999999999999, 'old_price': '12.99  EUR'}), 'HALTER NECK SHIMMER THREAD DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'V-NECK TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'DRESS WITH ZIPS': defaultdict(None, {'new_price': '25.95  EUR', 'saving': 7.960000000000001, 'old_price': '17.99  EUR'}), 'FLOUNCE DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'LONG TUNIC WITH SIDE VENTS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'FLOWING STUDIO DRESS': defaultdict(None, {'new_price': '89.95  EUR', 'saving': 39.96, 'old_price': '49.99  EUR'}), 'KNIT SWEATER WITH TAILORED HEM': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'BUTTON DETAIL DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'SUEDE EFFECT DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'FAUX LEATHER DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'POLKA DOT DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'CREPE SLEEVELESS DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'DEVORÉ VELVET DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'DENIM DRESS WITH BELT': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'LACE APPLIQUÉ DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'JACQUARD DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'PATCH PINAFORE DRESS': defaultdict(None, {'new_price': '45.95  EUR', 'saving': 19.960000000000004, 'old_price': '25.99  EUR'}), 'EYELET DRESS WITH VELVET RIBBON': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'LONG LACE DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 23.960000000000004, 'old_price': '25.99  EUR'}), 'LONG PATCHWORK DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'MIDI CROSSOVER DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'EMBROIDERED DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'LONG CREPE DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'HALTER NECK DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'DRESS WITH GATHERED WAIST': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'GINGHAM CHECK TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'JUMPSUIT WITH STRAPS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'DOUBLE LAYER DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'LACE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'MIDI PINAFORE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'LONG TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'BANDEAU DRESS': defaultdict(None, {'new_price': '25.95  EUR', 'saving': 7.960000000000001, 'old_price': '17.99  EUR'}), 'LINEN SHIRTDRESS BELT': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'STUDIO DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'CHECK PINAFORE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'LONG SILK STUDIO DRESS WITH LACE': defaultdict(None, {'new_price': '99.95  EUR', 'saving': 49.96, 'old_price': '49.99  EUR'}), 'BEAD EMBROIDERED DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 29.960000000000004, 'old_price': '29.99  EUR'}), 'DRESS WITH SIDE GATHERING': defaultdict(None, {'new_price': '19.95  EUR', 'saving': 6.959999999999999, 'old_price': '12.99  EUR'}), 'DRESS WITH BACK OPENING': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'SCHIFFLI LACE DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 23.960000000000004, 'old_price': '25.99  EUR'}), 'SPARKLY VELVET DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'STRAPPY DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'TUNIC WITH SLITS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'A-LINE PATCHWORK DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'DRESS WITH SEAM DETAILS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'COMBINATION DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'MICRO-JACQUARD DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'PINAFORE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'JACQUARD GEMSTONE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'VELVET DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'HOUNDSTOOTH PINAFORE DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'KNIT DRESS': defaultdict(None, {'new_price': '17.95  EUR', 'saving': 4.959999999999999, 'old_price': '12.99  EUR'}), 'LOOSE FIT PRINTED DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'LEATHER EFFECT DRESS. CUT WORK': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'GEOMETRIC DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'LONG DRESS': defaultdict(None, {'new_price': '25.95  EUR', 'saving': 7.960000000000001, 'old_price': '17.99  EUR'}), 'RETRO PINAFORE DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'CHECK DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'PRINTED SHIRT DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'PATTERNED DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'DRESS WITH CHAIN NECKLINE': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 29.960000000000004, 'old_price': '29.99  EUR'}), 'STAR PRINT DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'DENIM DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'ROUND NECK DRESS': defaultdict(None, {'new_price': '45.95  EUR', 'saving': 19.960000000000004, 'old_price': '25.99  EUR'}), 'FLORAL PRINT DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'RUFFLE DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'SHORT DRESS': defaultdict(None, {'new_price': '25.99  EUR', 'saving': 8.0, 'old_price': '17.99  EUR'}), 'PRINTED DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'FLARED DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'DENIM PINAFORE DRESS': defaultdict(None, {'new_price': '45.95  EUR', 'saving': 19.960000000000004, 'old_price': '25.99  EUR'}), 'ZIP-BACK TUBE DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'ASYMMETRIC DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'DRAPED DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'STRIPED JACQUARD DRESS': defaultdict(None, {'new_price': '25.95  EUR', 'saving': 7.960000000000001, 'old_price': '17.99  EUR'}), 'DRESS WITH SIDE KNOT DETAIL': defaultdict(None, {'new_price': '19.95  EUR', 'saving': 6.959999999999999, 'old_price': '12.99  EUR'}), 'OTTOMAN RIB DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'FLOWING TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'METALLIC APPLIQUÉ TUNIC': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'PATCHWORK DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'PARTY DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'FRAYED MIDI DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 9.96, 'old_price': '19.99  EUR'}), 'FLORAL DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'SKATER DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'DRESS WITH LOW-CUT BACK': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'LONG DOUBLE LAYER DRESS': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'LONG TUBE DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'FITTED DRESS': defaultdict(None, {'new_price': '14.95  EUR', 'saving': 4.959999999999999, 'old_price': '9.99  EUR'}), 'DRESS WITH V-NECK BACK': defaultdict(None, {'new_price': '29.95  EUR', 'saving': 11.96, 'old_price': '17.99  EUR'}), 'LONG VELVET DRESS': defaultdict(None, {'new_price': '89.95  EUR', 'saving': 39.96, 'old_price': '49.99  EUR'}), 'HALTER DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 9.960000000000004, 'old_price': '29.99  EUR'}), 'PRINTED TUNIC': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'FULL TUNIC WITH PLAITED BELT': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'STUDIO DRESS WITH LACE-UP BACK': defaultdict(None, {'new_price': '69.95  EUR', 'saving': 19.96, 'old_price': '49.99  EUR'}), 'DOTTED SWISS DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 23.960000000000004, 'old_price': '25.99  EUR'}), 'SHORT LACE DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'DRESS WITH FULL SKIRT': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'ASYMMETRICAL LONG DRESS': defaultdict(None, {'new_price': '17.95  EUR', 'saving': 4.959999999999999, 'old_price': '12.99  EUR'}), 'STRAIGHT CUT DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'MIDI DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'}), 'PLEATED DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 19.960000000000004, 'old_price': '29.99  EUR'}), 'LACE SKATER DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'TUBE DRESS WITH STRAPPY BACK': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'LONG PRINTED DRESS': defaultdict(None, {'new_price': '49.95  EUR', 'saving': 9.96, 'old_price': '39.99  EUR'}), 'SHEER DRESS': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 33.96000000000001, 'old_price': '25.99  EUR'}), 'A-LINE TOP': defaultdict(None, {'new_price': '59.95  EUR', 'saving': 9.96, 'old_price': '49.99  EUR'}), 'RIBBED DRESS': defaultdict(None, {'new_price': '17.95  EUR', 'saving': 4.959999999999999, 'old_price': '12.99  EUR'}), '"ESSENTIALS" DENIM PINAFORE DRESS': defaultdict(None, {'new_price': '19.95  EUR', 'saving': 6.959999999999999, 'old_price': '12.99  EUR'}), 'PRINTED JUMPSUIT DRESS': defaultdict(None, {'new_price': '39.95  EUR', 'saving': 13.960000000000004, 'old_price': '25.99  EUR'})})

如果您要分析数据,您可能还会发现pandas非常有用,如果您将数据存储在元组中,您可以轻松制作可以保存到csv并从csv加载的DataFrame。

data = []
for p in prods:
    k = p.a.text
    v1 = p.find("span", {"class": "crossOut"})["data-ecirp"]
    v2 = p.find("span", {"class": "sale"})["data-ecirp"]
    t = k, float(v1.split()[0]), float(v2.split()[0]), float(v1.split()[0]) - float(v2.split()[0])
    data.append(t)


import pandas as pd

df = pd.DataFrame(data, columns=["name", "old", "new", "saving"])
print(df)
df.to_csv("data.csv", index=0)

哪个会给你一个像:

                                 name    old    new  saving
0                 ZIP-BACK TUBE DRESS  49.95  29.99   19.96
1                     SEQUINNED DRESS  69.95  29.99   39.96
2    HALTER NECK SHIMMER THREAD DRESS  39.95  25.99   13.96
3              CREPE SLEEVELESS DRESS  49.95  29.99   19.96
4                   OTTOMAN RIB DRESS  39.95  25.99   13.96
5                    SHORT LACE DRESS  49.95  39.99    9.96
6                SPARKLY VELVET DRESS  49.95  29.99   19.96
7           DRESS WITH SIDE GATHERING  19.95  12.99    6.96
8        TUBE DRESS WITH STRAPPY BACK  59.95  49.99    9.96
9                        SKATER DRESS  49.95  39.99    9.96
10                        PARTY DRESS  49.95  29.99   19.96
11                 DOUBLE LAYER DRESS  29.95  17.99   11.96
12            DRESS WITH LOW-CUT BACK  49.95  39.99    9.96
13          DRESS WITH SIDE GATHERING  19.95  12.99    6.96
14                       STUDIO DRESS  79.95  49.99   29.96
15                 DOUBLE LAYER DRESS  59.95  49.99    9.96
16     STUDIO DRESS WITH LACE-UP BACK  69.95  49.99   19.96

将数据保存到名为data.csv的csv中。

以最大的节省来加载数据和订单:

df = pd.read_csv("data.csv")


print(df.sort_values(["saving"],ascending=0))

哪个会输出:

                                  name    old    new  saving
134   LONG SILK STUDIO DRESS WITH LACE  99.95  49.99   49.96
1                      SEQUINNED DRESS  69.95  29.99   39.96
137                  LONG VELVET DRESS  89.95  49.99   39.96
71                FLOWING STUDIO DRESS  89.95  49.99   39.96
33                  LONG CROCHET DRESS  89.95  49.99   39.96
116                        SHEER DRESS  59.95  25.99   33.96
85           DRESS WITH CHAIN NECKLINE  59.95  29.99   29.96
132             BEAD EMBROIDERED DRESS  59.95  29.99   29.96
14                        STUDIO DRESS  79.95  49.99   29.96
......................................................

添加应保存百分比的列:

df["precent_saving"] = (df["old"] - df["new"]) / df["old"] * 100
print(df)

输出:

                               name    old    new  saving  precent_saving
0                 ZIP-BACK TUBE DRESS  49.95  29.99   19.96       39.959960
1                     SEQUINNED DRESS  69.95  29.99   39.96       57.126519
2    HALTER NECK SHIMMER THREAD DRESS  39.95  25.99   13.96       34.943680
3              CREPE SLEEVELESS DRESS  49.95  29.99   19.96       39.959960
4                   OTTOMAN RIB DRESS  39.95  25.99   13.96       34.943680
5                    SHORT LACE DRESS  49.95  39.99    9.96       19.939940
6                SPARKLY VELVET DRESS  49.95  29.99   19.96       39.959960
7           DRESS WITH SIDE GATHERING  19.95  12.99    6.96       34.887218
8        TUBE DRESS WITH STRAPPY BACK  59.95  49.99    9.96       16.613845
9                        SKATER DRESS  49.95  39.99    9.96       19.939940
10                        PARTY DRESS  49.95  29.99   19.96       39.959960
11                 DOUBLE LAYER DRESS  29.95  17.99   11.96       39.933222