Skip to content

Instantly share code, notes, and snippets.

@impshum
Created August 10, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impshum/e869bbf355f9ad07a221d4971f4266ad to your computer and use it in GitHub Desktop.
Save impshum/e869bbf355f9ad07a221d4971f4266ad to your computer and use it in GitHub Desktop.
An attempt to grab selected elements from pages (fuck that: just build it).
from bs4 import BeautifulSoup
import requests
import cssutils
import logging
cssutils.log.setLevel(logging.FATAL)
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'}
base_url = 'http://localhost:8888/bulma-start/'
def lovely_soup(url):
r = requests.get(url, headers=headers)
return BeautifulSoup(r.content, 'lxml')
def lovely_content(url):
r = requests.get(url, headers=headers)
return r.content
soup = lovely_soup(base_url)
header = soup.find('nav', {'class': 'navbar'})
footer = soup.find('footer', {'class': 'footer'})
all_class_items = ['navbar', 'footer']
all_tag_items = ['nav', 'footer']
items = [header, footer]
for item in items:
for child in item.findChildren(recursive=True):
if child.attrs:
if 'class' in child.attrs:
for c in child.attrs['class']:
if c not in all_class_items:
all_class_items.append(c)
styles = []
for style in soup.find('head').find_all('link', href=True):
href = style['href']
if href.endswith('.css'):
styles.append(f'{base_url}{href}')
all_styles = ''
for style in styles:
css = lovely_content(style)
sheet = cssutils.parseString(css)
for rule in sheet:
if rule.type == rule.STYLE_RULE:
for tag_item, class_item in zip(all_tag_items, all_class_items):
if tag_item in rule.selectorText or f'.{class_item}' in rule.selectorText:
properties = ''
for property in rule.style:
properties += f'{property.name}: {property.value};\n'
all_styles += rule.selectorText + \
' {\n ' + properties + '}' + '\n\n'
with open('data/header.html', 'w') as head, open('data/footer.html', 'w') as foot, \
open('data/styles.css', 'w') as styles, open('data/demo.html', 'w') as demo:
head.write(str(header))
foot.write(str(footer))
styles.write(all_styles)
demo_html = f'''<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>DEMO</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- HEADER -->
{str(header)}
<!-- END HEADER -->
<!-- FOOTER -->
{str(footer)}
<!-- END FOOTER -->
<!--script src="js/scripts.js"></script-->
</body>
</html>
'''
demo.write(demo_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment