python - 如何从“data-at”中提取文本?
问题描述
我正在尝试抓取丝芙兰的网站,但我无法获得我想要的信息,我正在尝试提取每种香水的名称,我尝试了 2 种方法,使用“brand=soup.find(..... )[...]" 并将其从循环中取出,然后返回“sku_item_brand”,这不是我想要的,第二种方法是下面的代码,但我不知道为什么它在环形。我得到的错误是“NoneType”对象不可下标有人请帮忙!
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")
# List to store the scraped data in
brands = []
for container in perfume_containers:
# The brand
brand = container.find('span', class_='css-ktoumz')['data-at']
brands.append(brand)
解决方案
brand.text 将返回文本值
for container in perfume_containers:
brand = container.find('span', class_='css-ktoumz')
try:
brands.append(brand.text)
except AttributeError:
continue
print(brands)
输出
['CHANEL', 'Viktor&Rolf', 'CHANEL', 'Juliette Has a Gun', 'TOM FORD', 'CHANEL', 'Yves Saint Laurent', 'Versace', 'Yves Saint Laurent', 'Chloé', 'Sephora Favorites', 'Valentino']
推荐阅读
- c - 无法在 M1 Mac 上使用 VSC 调试 C 程序
- sql - Oracle 难以创建具有子查询的过程
- xml - 谷歌 IMPORTXML 将 Shopee 价格抓取到谷歌表
- python - 如何获取 NetworkX 图形集中的图形数量?
- javascript - 如何在 html 代码中注入 javascript 函数代码?
- prolog - 术语的安全排序
- php - Laravel orWherehas 过滤多列关系
- r - R bslib 更改导航栏颜色引导版本 5
- javascript - 使用参数访问网站时出现404错误(vuejs)
- javascript - JS - 正则表达式适用于 regex101 但不适用于我的节点脚本