如何使用BeautifulSoup从网站获取var脚本中的json数据?

帕特里克·皮尔扎诺夫斯基(Patrik Pyrzanowski)

我正在尝试获取在检查页面源中的特定URL时看到的JSON数据。这是网址:https : //www.uniqlo.com/de/de/product/ut-bedrucktes-t-shirt-kaws-summer-421771.html

我的目标是获取“ ats”的“定价”和“可用性”:“ xxx”

这是源代码的一部分:

</div>
<div class="product-detail">

<script>
var pdpVariationsJSON = 

{"color-COL08|size-SMA002":{"id":"421771COL08SMA002000","attributes":{"color":"Dunkelgrau","size":"XS"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"203","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA003":{"id":"421771COL08SMA003000","attributes":{"color":"Dunkelgrau","size":"S"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"106","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA004":{"id":"421771COL08SMA004000","attributes":{"color":"Dunkelgrau","size":"M"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"218","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA005":{"id":"421771COL08SMA005000","attributes":{"color":"Dunkelgrau","size":"L"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"7","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA006":{"id":"421771COL08SMA006000","attributes":{"color":"Dunkelgrau","size":"XL"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"83","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA007":{"id":"421771COL08SMA007000","attributes":{"color":"Dunkelgrau","size":"XXL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA008":{"id":"421771COL08SMA008000","attributes":{"color":"Dunkelgrau","size":"3XL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA002":{"id":"421771COL68SMA002000","attributes":{"color":"Blau","size":"XS"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"159","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA003":{"id":"421771COL68SMA003000","attributes":{"color":"Blau","size":"S"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA004":{"id":"421771COL68SMA004000","attributes":{"color":"Blau","size":"M"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"63","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA005":{"id":"421771COL68SMA005000","attributes":{"color":"Blau","size":"L"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"9","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA006":{"id":"421771COL68SMA006000","attributes":{"color":"Blau","size":"XL"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"136","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA007":{"id":"421771COL68SMA007000","attributes":{"color":"Blau","size":"XXL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA008":{"id":"421771COL68SMA008000","attributes":{"color":"Blau","size":"3XL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}}}
;
</script>

<div id="product-content">

我已经尝试过使用json.loads()了,但这给了我这个错误:

“从无json.decoder.JSONDecodeError提高JSONDecodeError(“期望值”,s,err.value):期望值:第1行第1列(字符0)“

到目前为止,这是我的代码片段的样子:

import requests

from bs4 import BeautifulSoup

import json

url = "https://www.uniqlo.com/de/de/product/ut-bedrucktes-t-shirt-kaws-summer-421771.html"

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

response = requests.get(url, headers=headers)

soup = BeautifulSoup(response.text, "lxml")



div = soup.find("div", class_="product-detail")

scr = str(div.find("script"))

print(scr)

这是我现在在打印时得到的结果:

<script>
var pdpVariationsJSON = 

{"color-COL08|size-SMA002":{"id":"421771COL08SMA002000","attributes":{"color":"Dunkelgrau","size":"XS"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"203","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA003":{"id":"421771COL08SMA003000","attributes":{"color":"Dunkelgrau","size":"S"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"106","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA004":{"id":"421771COL08SMA004000","attributes":{"color":"Dunkelgrau","size":"M"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"218","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA005":{"id":"421771COL08SMA005000","attributes":{"color":"Dunkelgrau","size":"L"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"7","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA006":{"id":"421771COL08SMA006000","attributes":{"color":"Dunkelgrau","size":"XL"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"83","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA007":{"id":"421771COL08SMA007000","attributes":{"color":"Dunkelgrau","size":"XXL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL08|size-SMA008":{"id":"421771COL08SMA008000","attributes":{"color":"Dunkelgrau","size":"3XL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA002":{"id":"421771COL68SMA002000","attributes":{"color":"Blau","size":"XS"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"159","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA003":{"id":"421771COL68SMA003000","attributes":{"color":"Blau","size":"S"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA004":{"id":"421771COL68SMA004000","attributes":{"color":"Blau","size":"M"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"63","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA005":{"id":"421771COL68SMA005000","attributes":{"color":"Blau","size":"L"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"9","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA006":{"id":"421771COL68SMA006000","attributes":{"color":"Blau","size":"XL"},"availability":{"status":"IN_STOCK","statusQuantity":"0","inStock":true,"ats":"136","inStockDate":"","availableForSale":true,"purchaseLevel":"","levels":{"IN_STOCK":1,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":0},"isAvailable":true,"inStockMsg":"1 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA007":{"id":"421771COL68SMA007000","attributes":{"color":"Blau","size":"XXL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}},"color-COL68|size-SMA008":{"id":"421771COL68SMA008000","attributes":{"color":"Blau","size":"3XL"},"availability":{"status":"NOT_AVAILABLE","statusQuantity":"0","inStock":false,"ats":"0","inStockDate":"","availableForSale":false,"purchaseLevel":"","levels":{"IN_STOCK":0,"PREORDER":0,"BACKORDER":0,"NOT_AVAILABLE":1},"isAvailable":false,"inStockMsg":"0 Produkt(e) verfügbar","preOrderMsg":"0 Produkt(e) sind zur Vorbestellung verfügbar.","backOrderMsg":"Back Order 0 item(s)"},"pricing":{"showStandardPrice":false,"isPromoPrice":false,"standard":14.9,"formattedStandard":"14,90 €","sale":14.9,"formattedSale":"14,90 €","salePriceMoney":{},"standardPriceMoney":{},"quantities":[{"unit":"","value":0}]}}}
;
</script>
安德烈·凯斯利(Andrej Kesely)

您可以用来re<script>标记中提取重要部分并用于json.loads解析它:

import re
import json
import requests
from bs4 import BeautifulSoup

url = "https://www.uniqlo.com/de/de/product/ut-bedrucktes-t-shirt-kaws-summer-421771.html"

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "lxml")

div = soup.find("div", class_="product-detail")
scr = str(div.find("script"))

data = json.loads(re.findall(r'pdpVariationsJSON[^{]+(.*);', str(scr), flags=re.DOTALL)[0])

print(json.dumps(data, indent=4))

印刷品:

{
    "color-COL08|size-SMA002": {
        "id": "421771COL08SMA002000",
        "attributes": {
            "color": "Dunkelgrau",
            "size": "XS"
        },
        "availability": {
            "status": "IN_STOCK",
            "statusQuantity": "0",
            "inStock": true,
            "ats": "203",
            "inStockDate": "",
            "availableForSale": true,
            "purchaseLevel": "",
            "levels": {
                "IN_STOCK": 1,
                "PREORDER": 0,
                "BACKORDER": 0,
                "NOT_AVAILABLE": 0
            },
            "isAvailable": true,
            "inStockMsg": "1 Produkt(e) verf\u00fcgbar",
            "preOrderMsg": "0 Produkt(e) sind zur Vorbestellung verf\u00fcgbar.",
            "backOrderMsg": "Back Order 0 item(s)"
        },
        "pricing": {
            "showStandardPrice": false,
            "isPromoPrice": false,
            "standard": 14.9,
            "formattedStandard": "14,90 \u20ac",
            "sale": 14.9,
            "formattedSale": "14,90 \u20ac",
            "salePriceMoney": {},
            "standardPriceMoney": {},
            "quantities": [
                {
                    "unit": "",
                    "value": 0
                }
            ]
        }
    },
    "color-COL08|size-SMA003": {
        "id": "421771COL08SMA003000",
        "attributes": {
            "color": "Dunkelgrau",
            "size": "S"
        },
        "availability": {
            "status": "IN_STOCK",
            "statusQuantity": "0",
            "inStock": true,
            "ats": "106",
            "inStockDate": "",
            "availableForSale": true,
            "purchaseLevel": "",
            "levels": {
                "IN_STOCK": 1,
                "PREORDER": 0,
                "BACKORDER": 0,
                "NOT_AVAILABLE": 0
            },
            "isAvailable": true,
            "inStockMsg": "1 Produkt(e) verf\u00fcgbar",
            "preOrderMsg": "0 Produkt(e) sind zur Vorbestellung verf\u00fcgbar.",
            "backOrderMsg": "Back Order 0 item(s)"
        },
        "pricing": {
            "showStandardPrice": false,
            "isPromoPrice": false,
            "standard": 14.9,
            "formattedStandard": "14,90 \u20ac",
            "sale": 14.9,
            "formattedSale": "14,90 \u20ac",
            "salePriceMoney": {},
            "standardPriceMoney": {},
            "quantities": [
                {
                    "unit": "",
                    "value": 0
                }
            ]
        }
    },

... and so on.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Ajax方法获取网站数据

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

如何使用BeautifulSoup从网站上获取所有标头?

我如何使用beautifulsoup从网站的表格中获取多个信息

如何使用LibreOffice的Calc从网站获取数据?

如何使用BeautifulSoup在网站上获取实时股价?

使用BeautifulSoup提取网站数据

如何在脚本函数中获取var?

使用Beautifulsoup从网站提取数据

从网站的搜索结果中获取列表,方法是使用beautifulsoup,然后将数据转换为数据框

使用BeautifulSoup从网站获取表格

使用beautifulsoup从脚本标签中抓取数据

如何使用BeautifulSoup从网站获取href链接

如何使用beautifulsoup-授权问题从图表中获取数据?

如何使用beautifulsoup从html标记的特定类中获取数据?

如何使用JavaScript从网站获取JSON?

使用Python从网站中获取HTML数据

如何使用相同的 PHP 脚本为移动应用程序和网站获取数据?

如何使用python中的BeautifulSoup库从具有“查看更多”选项的网站上抓取数据

如何使用 BeautifulSoup 获取网站图片 src

如何使用 PHP 从 JSON 中获取数据

如何使用 BeautifulSoup Python 获取数据表单网站?

如何使用 php 脚本从 MySQL 数据库中获取数据到嵌套的 JSON 数组中?

如何使用python中的beautifulsoup从网页中获取数据

使用python和beautifulsoup从BBB网站获取数据

使用 Google 表格中的应用脚本获取网站数据

如何使用BeautifulSoup从python网站中未加载的选项卡中抓取表格数据

如何使用beautifulsoup从带有var的<script>获取数据?

使用 BeautifulSoup 从表中获取数据