python - 用python抓取网页的所有颜色

标签 python css colors screen-scraping

我想找到一种有效的方法来使用 python 从给定的页面 url 中提取某种调色板(列表或其他内容)。我想要的是采用所有背景颜色、标题颜色和所有其他元素。

我已经在这里看到了 [ Build a color palette from image URL ] 可以从图像中获取调色板,但是页面呢?

最佳答案

是否将 selenium 与上面的示例混合在一起。 下面的示例展示了如何从 Google 的搜索中获取前十种颜色。

只需用网络爬虫截取网页,然后对图像进行处理

#!/bin/env python3
from selenium import webdriver
import numpy as np
from PIL import Image

def palette(img):
    """
    Return palette in descending order of frequency
    """
    arr = np.asarray(img)
    palette, index = np.unique(asvoid(arr).ravel(), return_inverse=True)
    palette = palette.view(arr.dtype).reshape(-1, arr.shape[-1])
    count = np.bincount(index)
    order = np.argsort(count)
    return palette[order[::-1]]

def asvoid(arr):
    """View the array as dtype np.void (bytes)
    This collapses ND-arrays to 1D-arrays, so you can perform 1D operations on them.
    http://stackoverflow.com/a/16216866/190597 (Jaime)
    http://stackoverflow.com/a/16840350/190597 (Jaime)
    Warning:
    >>> asvoid([-0.]) == asvoid([0.])
    array([False], dtype=bool)
    """
    arr = np.ascontiguousarray(arr)
    return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))


def savePrint(imageFile):
    driver = webdriver.Firefox()
    driver.get("https://google.com.br")    
    driver.get_screenshot_as_file(imageFile)

imageFile = '/tmp/tmp.png'
savePrint(imageFile)
img = Image.open(imageFile, 'r').convert('RGB')
print(palette(img)[:10])

关于python - 用python抓取网页的所有颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910155/

相关文章:

python - 如何使用 pybrain 等外部 python 库在 cython 中编译我的 python 代码

python - Scrapyd:将 CSV 文件写入远程服务器

python - 如何正确使用堆对 priorityqueue 进行双端队列和排序?

html - 使用 HTML 表单提交对象的 JSON 数组

html - 如何设置包含的 div 之间的空间?

启动时的 Java Jtable 行颜色

swift - 如何在 SwiftUI 中将属性定义为 Color 或 LinearGradient?

django 中的 python 模块未正确导入

html - 不需要的多个背景之前/之后的 CSS

java - 如何删除 JComboBox 的背景?