-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapyColorList.py
executable file
·40 lines (32 loc) · 1.14 KB
/
ScrapyColorList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from bs4 import BeautifulSoup
import lxml
import requests
import re
req = requests.get('http://cloford.com/resources/colours/500col.htm').content
soup = BeautifulSoup(req, 'lxml')
colors = {}
hexRe = re.compile(r'(#[A-Z0-9]+)')
rgbRe = re.compile(r'(\s(\d+)\s(\d+)\s(\d+))')
nameRe = re.compile(r'\s*([a-zA-Z]+\s*[a-zA-Z]*\s*\d*)\s*')
colorsList = []
table = soup.findAll('tr')
count = 0
outFile = open('colors.py', 'w+')
for row in table:
nameMatch = nameRe.search(row.text)
hexMatch = hexRe.search(row.text)
rgbMatch = rgbRe.search(row.text)
if nameMatch and rgbMatch and hexMatch:
name = nameMatch.group(1).split('\n')[0]
name = name.replace(' ', '').upper()
r, g, b = rgbMatch.group(2), rgbMatch.group(3), rgbMatch.group(4)
hex = hexMatch.group(1)
hex = str(hex)
line = "{} = ({}, {}, {}, '{}')\n".format(name.replace(' ', '').upper(), r, g, b, hex)
colorsList.append((name, int(r), int(g), int(b), hex))
colorsList.pop(0)
colorsSet = set(colorsList)
colorsList = list(colorsSet)
for row in sorted(colorsList):
line = "{} = ({}, {}, {}, '{}')\n".format(*row)
outFile.write(line)