使用美丽的汤来找到目标“物品”

问题描述 投票:0回答:1

所以我试图在丝芙兰网站上取消香水的名称,品牌和价格。但是我注意到,只有60种香水中的前12种会出现(一页上有60种香水)。我尝试打印出“ item_container”的长度,结果显示其中有60个,但是从第12个项目开始,一些结构不同的代码开始出现。我已经检查了它们的HTML结构,但我不明白为什么我的代码对其余的代码无效。我还试图将“类”更改为更具体的类,例如:

perfume_containers = soup.find_all('div', class_="css-12egk0t")

to

perfume_containers = soup.find_all('div', class_="css-ix8km1")

但是它要么给我相同的结果,要么什么也回不来。HTML code of item that is not showing up

HTML code of item that works

这是我的代码,我只展示提取品牌的部分,因为要展示整个内容太长了。请发送一些帮助!谢谢!

import pandas as pd
from bs4 import BeautifulSoup
import requests
source = requests.get('https://www.sephora.com/shop/perfume')
soup = BeautifulSoup(source.content, 'html.parser')
perfume_containers = soup.find_all('div', class_="css-12egk0t")
brands = []
for container in perfume_containers:
# The brand
    brand = container.find('span', class_='css-ktoumz')
    try:
        brands.append(brand.text)
    except:
        continue
python html web-scraping
1个回答
0
投票

这里的问题是,如从html属性data-lload="comp"中注意到的,页面加载后会加载一些类数据,而您成功抓取的12个项目具有属性data-lload="false"。 BeautifulSoup准确地解析html,您如何看待网页的源代码,并且您可以从源代码中看到仅加载了12个项目,因此其余项目可能以其他方式(也许使用了ajax或其他方式)进行加载,但是在这种情况下案例我发现项目实际上是在源底部的script标签上以json的形式在源上交付的,因此您实际上不再需要抓取数据了,您可以按如下所示直接访问json:

import pandas as pd
from bs4 import BeautifulSoup
import requests
import json

source = requests.get('https://www.sephora.com/shop/perfume')
soup = BeautifulSoup(source.content, 'html.parser')

scriptContent = soup.find(id="linkJSON").text

catalog = json.loads(scriptContent)

products = catalog[3]['props']['products']

extracted = []
for p in products:
  extracted.append({'brand': p['brandName'], 'displayName': p['displayName'], 'price': p['currentSku']['listPrice']})

print(extracted)
© www.soinside.com 2019 - 2024. All rights reserved.