【问题标题】:Grab all colors of a webpage with python用python抓取网页的所有颜色
【发布时间】:2019-09-18 11:29:58
【问题描述】:

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

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

【问题讨论】:

  • 这不是一件容易的事。页面(和广告)的动态内容使一切变得更加困难。您可能需要检查stackoverflow.com/questions/1587637/…,然后您应该将页面转换为图像。

标签: python css colors screen-scraping


【解决方案1】:

将硒与上面的示例混合在一起。 下面的示例展示了如何从 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])

【讨论】:

  • 我正在寻找可以报告调色板和元素名称的东西(例如,“所有 h2 都是黄色”,“div id_div”有蓝色背景”...) ,但这是一个很好的起点!
  • 可以遍历所有元素并进行上述相同的过程,但不知道这是否是最佳解决方案,因为它会截取太多屏幕截图,并且会成为性能杀手。
【解决方案2】:

我尝试了以下方法,这对我有用:想法是使用 selenium 访问页面源,然后我搜索所有以 'value_of_css_property 并搜索背景颜色、边框颜色、颜色、背景图像。我知道这并不完美,但它可以满足我的需求。不要忘记从标签列表中删除重复项(因为此方法将给出每个标签的所有 css-color 属性的列表)。 示例:

url ="someurl"
options = webdriver.ChromeOptions()
options.headless = False
driver = webdriver.Chrome(options=options)
driver.get(url)
list_tags = []
html_source = driver.page_source
txt = re.findall(r'<[a-zA-Z]+', html_source)
for x in txt:
    list_tags.append(x.replace('<', ''))
list_tags = list(dict.fromkeys(list_tags))
final_list = []

for i in list_tags:
 tag = driver.find_elements_by_tag_name(i)
 tag_back_col = []
 tag_col = []
 tag_img = []
 tag_border = []
 for j in tag:
      back_col = j.value_of_css_property('background-color')
      tag_back_col.append(back_col)
      col = j.value_of_css_property('color')
      tag_col.append(col)
      bord = j.value_of_css_property('border-color')
      tag_border.append(bord)
      img = j.value_of_css_property('background-image')
      tag_img.append(img)
  final_list .append((i, tag_back_col, tag_col, tag_border, tag_img))
driver.close()

最终列表将是带有标签名称的元组列表以及页面中每次出现该标签的背景颜色、颜色、边框颜色和背景图像列表。

【讨论】:

    猜你喜欢
    • 2022-11-02
    • 2021-09-20
    • 2021-10-10
    • 2019-01-15
    • 1970-01-01
    • 2021-01-12
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多