Skip to content

Instantly share code, notes, and snippets.

@mbadros
Created August 4, 2020 22:43
Show Gist options
  • Select an option

  • Save mbadros/f45b70bb7001a004dbab805176cab2d5 to your computer and use it in GitHub Desktop.

Select an option

Save mbadros/f45b70bb7001a004dbab805176cab2d5 to your computer and use it in GitHub Desktop.
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