首页 > 解决方案 > 从“aria-label”标签中提取属性值

问题描述

我正在尝试从丝芙兰的网站上提取产品的评级。但是,当我尝试提取评级时,它并没有按照我想要的方式工作。我想我的问题是我不知道如何定位那个标签 b/c 它与我需要提取的其他数据相比具有不同的结构。请帮忙!

这是网站:https ://www.sephora.com/product/revitalizing-supreme-global-anti-aging-creme-P384342?icid2=products%20grid:p384342

这里的图片是我想要提取的值。 在此处输入图像描述

我还在这里附上了我的代码。

final_products = [] #empty list to append the dictionary in to before passing in to a DataFrame
for i in range(0, len(link_list)):
#for i in urls.index:
     current_url = link_list[i]
     resource = requests.get(current_url)
     current_data = resource.text
     soup = BeautifulSoup(current_data, 'html.parser')
     #gathering the data from the pages
     try:
        product = {}
        product['Name'] = soup.find('span', {'class': 'css-0'}).text
        product['Price'] = soup.find('div', {'class': 'css-slwsq8'}).text
        product['Num_of_reviews'] = soup.find_all('span', {'class': 'css-2rg6q7'})[0].text
        product['Num_of_Likes'] = soup.find('span', {'data-at': 'product_love_count'}).text
        product['Rating'] = soup.find('a', {'aria-label'})
        #append the empty list to later make in to a dataframe
        final_products.append(product)
    except: 
        pass

标签: pythonhtmlweb-scraping

解决方案


推荐阅读