Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CSS Background Colour with Python?

Basically exactly as the question says, I'm trying to get the background colour out from a website.

At the moment I'm using BeautifulSoup to get the HTML, but it's proving a difficult way of the getting the CSS. Any help would be great!

like image 334
user3015035 Avatar asked May 20 '26 09:05

user3015035


1 Answers

This is not something you can reliably solve with BeautifulSoup. You need a real browser.

The simplest option would be to use selenium browser automation tool:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('url')

element = driver.find_element_by_id('myid')
print(element.value_of_css_property('background-color'))

value_of_css_property() documentation.

like image 95
alecxe Avatar answered May 22 '26 23:05

alecxe