-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build a minimalistic Click cli around scraper
- Loading branch information
Showing
1 changed file
with
33 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
#!/usr/bin/env python | ||
|
||
import click | ||
from bs4 import BeautifulSoup | ||
import requests | ||
import re | ||
import pprint as p | ||
|
||
@click.command() | ||
@click.option( | ||
'--output', '-o', default='default', type=click.Choice(['default', 'csv']), | ||
show_choices=True, help=f'''Output format "default" prints human readable | ||
on shell, "csv" is a two-column comma separated value format.''') | ||
@click.argument('URL') | ||
def scrape(output, url): | ||
'''Scrape one chapter of Admin API docs and spit out in various formats. | ||
chapter = 'https://element-hq.github.io/synapse/develop/admin_api/rooms.html' | ||
apidoc = requests.get(chapter).text | ||
soup = BeautifulSoup(apidoc, 'html.parser') | ||
URL is the address of the Synapse Admin API docs chapter. For example: | ||
https://element-hq.github.io/synapse/develop/admin_api/rooms.html''' | ||
chapter = url | ||
apidoc = requests.get(chapter).text | ||
soup = BeautifulSoup(apidoc, 'html.parser') | ||
|
||
elements = soup.find_all( | ||
['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a'], | ||
) | ||
elements = soup.find_all( | ||
['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a'], | ||
) | ||
|
||
#p.pprint(elements) | ||
for e in elements: | ||
if e.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: | ||
print(f'HEADLINE {e.name}: {e.text}') | ||
if e.name == 'a': | ||
if e.parent.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: | ||
link = e['href'] | ||
print(f'{e.text} {link}') | ||
print() | ||
|
||
print() | ||
print() | ||
print() | ||
#p.pprint(elements) | ||
for e in elements: | ||
if e.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: | ||
print(f'HEADLINE {e.name}: {e.text}') | ||
if e.name == 'a': | ||
if e.parent.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']: | ||
link = e['href'] | ||
print(f'{e.text} {link}') | ||
print() | ||
|
||
print() | ||
print() | ||
print() | ||
#print(soup.prettify()) | ||
|
||
|
||
if __name__ == '__main__': | ||
scrape() |