这是我当前的代码,我试图从下面的站点中导入span类中样式中的所有“背景色rgb和值”。
https://www.asianpaints.com/colour/colour-catalogue.html
import requests
from bs4 import BeautifulSoup as bs
import os
URL = "https://www.asianpaints.com/colour/colour-catalogue.html"
r = requests.get(URL)
collec = bs(r.content, 'lxml')
color= collec.find_all(class_='color-box3')
print(color)
答案 0 :(得分:2)
RGB值全部使用Javascript完成,而Python或BeautifulSoup不会处理这些Javascript,这就是为什么您无法在返回的HTML中看到所需信息的原因。
另一种简便的方法是发现网页发出的请求,以JSON响应的形式获取颜色列表。然后,可以使用json()
请求函数将数据轻松转换为Python字典,然后将其作为Python字典进行访问:
import requests
URL = "https://www.asianpaints.com/content/ap/en/home/colour/colour-catalogue/jcr:content/oneColumnParsys/colourcatalog.colourfamily.json"
data = requests.get(URL).json()
for shade in data['shades']:
name = shade['shadeName']
rgb = f"({shade['shadeR']}, {shade['shadeG']}, {shade['shadeB']})"
print(f"{name} - {rgb}")
为您提供各种阴影,如下所示:
Raven Song - (64, 64, 64)
Platinum Blue - (56, 61, 103)
Black Currant - (65, 64, 67)
Stormy Sky - (58, 65, 80)
Armada - (55, 70, 91)
Navy Blue - (56, 67, 90)
Blue Mountain - (51, 73, 95)
Rich Berry - (77, 67, 72)
在Python 3.6.6上测试