使用 BeautifulSoup4 的 Python Web 抓取跨度标签以获取英镑价格

标签 python html regex web-scraping

第一次发帖,请多多关照。我正在尝试使用 Python 进行网络抓取。

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

“WWWzaraDOTcom/uk/en/sale/woman/dresses/view-all-c731533.html”

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

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

我下面的当前代码可以访问 Zara,拉取页面并在 RE/BS 中进行操作 - 但在多次谷歌搜索 BeautifulSoup 和 span 标签后我无法拉出任何价格。

Regex 有点伤了我的脑袋,我无法从我的书中得到任何简单的代码示例来工作 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')
    

最佳答案

你想要的所有数据都在product-info标签中,url也不同,因为你必须先选择一个地区:

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
........................

将数据存储在字典中可能是个好主意:

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如果您要分析数据,这很有用,如果您将数据存储在元组中,则可以轻松制作一个 DataFrame,您可以将其保存到 csv 中并从中加载。

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)

这会给你一个像这样的 df:

                                 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

关于使用 BeautifulSoup4 的 Python Web 抓取跨度标签以获取英镑价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34482901/

相关文章:

javascript - AngularJs:如何使 ui-select 正常工作?

javascript - 允许仅键入浮点百分比值作为输入

Java 正则表达式查找除 B 之外的所有 A

python - 如何替换pandas数据框中的字符?

python - 从Pandas DataFrame中获取最大值的行索引和列索引

javascript - html/js 中从右到左和/或从上到下的文本?

javascript - 如何在复选框上使用 addClass 仅定位 div 中的 ID

python - 在 Python 正则表达式中捕获重复子模式

python - 如何用 bash 脚本杀死 python 脚本

python - pandas 中的 tz_convert 不适用于印度的任何城市