Created
August 4, 2020 22:43
-
-
Save mbadros/f45b70bb7001a004dbab805176cab2d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import requests | |
| import lxml.html as lh | |
| import pandas as pd | |
| header = { "User-Agent"; #.... | |
| r = requests.get(url, headers=header) | |
| df = pd.read_html(r.text)[0] | |
| ###################################### | |
| # Create page to handle the contents of the website | |
| page = requests.get(url) | |
| # Store the contents of the website under doc | |
| doc = lh.fromstring(page.content) | |
| # Parse data that are stored between <tr>..</tr> of HTML | |
| tr_elements = doc.xpath('//tr') | |
| for T in tr_elements[1: ]: | |
| for i, t in enumerate(T.iterchildren()): | |
| data = t.text_content() | |
| col[i][1].append(int(data) if data.isnumeric() else data) | |
| Dict = {title:columne for (title, column) in col} | |
| df = pd.DataFrame(Dict) | |
| print(df.head()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment