|
| 1 | +from bs4 import BeautifulSoup as soup |
| 2 | +import requests |
| 3 | +import random |
| 4 | + |
| 5 | + |
| 6 | +SUBLIME_SNIPPET = """ |
| 7 | +<snippet> |
| 8 | + <content><![CDATA[{0}${1}]]></content> |
| 9 | + <description> /{2}</description> |
| 10 | + <tabTrigger>{3}</tabTrigger> |
| 11 | +</snippet> |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +TABLES = [ |
| 16 | + dict(html_name='table sectionedit4', var_name="SETUP"), # The SETUP Section |
| 17 | + dict(html_name='table sectionedit6', var_name="STEP"), # The STEP Section |
| 18 | + dict(html_name='table sectionedit8', var_name="VARIABLES"), # Variables usable in SETUP and STEP Section |
| 19 | + dict(html_name='table sectionedit10', var_name="DATA_TYPES"), # Data types (user input) |
| 20 | + dict(html_name='table sectionedit15', var_name="DATA_EXTRACTION"), # Data Extraction |
| 21 | +] |
| 22 | + |
| 23 | +HTML = requests.get('https://docu.gsa-online.de/search_engine_ranker/script_manual').content |
| 24 | +PARSER = soup(HTML, 'lxml') |
| 25 | +RESULTS = [] |
| 26 | + |
| 27 | + |
| 28 | +def parse_table(data: dict): |
| 29 | + _html_name = data['html_name'] |
| 30 | + |
| 31 | + rows = PARSER.findAll('div', **dict(class_=_html_name))[0].find('table').find_all('tr')[1:] |
| 32 | + |
| 33 | + _return = [] |
| 34 | + |
| 35 | + for r in rows: |
| 36 | + a = r.find_all('td') |
| 37 | + v = a[0].text.rstrip() # variable |
| 38 | + d = a[1].text.rstrip()\ |
| 39 | + .replace('<', '<')\ |
| 40 | + .replace('>', '>')\ |
| 41 | + .replace("'", "'")\ |
| 42 | + .replace('"', """)\ |
| 43 | + .replace("&", "&") # description |
| 44 | + _return.append({v: d}) |
| 45 | + print(d) |
| 46 | + return _return |
| 47 | + |
| 48 | + |
| 49 | +for table in TABLES: |
| 50 | + need = parse_table(table) |
| 51 | + print(table['var_name'], need) |
| 52 | + |
| 53 | + for x in need: |
| 54 | + for k, v in x.items(): |
| 55 | + data = SUBLIME_SNIPPET.format((k+'=').lower(), '{1}', v, k).replace('\n', '').replace('\t', '') |
| 56 | + file_name = './sublime_snippets/{0}.sublime-snippet'.format(k |
| 57 | + .replace('\n', '') |
| 58 | + .replace('\t', '') |
| 59 | + .replace('/', '') |
| 60 | + .replace('\\', '')) |
| 61 | + # write snippet |
| 62 | + file = open(file_name, 'w+', encoding='utf-8') |
| 63 | + file.write(data) |
| 64 | + file.close() |
| 65 | + # write help |
| 66 | + file2_name = 'help_sublime_snippets.ini' |
| 67 | + file2 = open(file2_name, 'a', encoding='utf-8') |
| 68 | + file2.write(f"{k}\n{v}\n########################################\n") |
| 69 | + file2.close() |
0 commit comments