- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Read HTML Page.py
More file actions
Latest commit
33 lines (28 loc) · 942 Bytes
/
Copy pathPython Read HTML Page.py
File metadata and controls
33 lines (28 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
importrequestsasrq
frombs4importBeautifulSoup
importpandasaspd
url='https://en.wikipedia.org/wiki/List_of_National_Football_League_career_scoring_leaders'
rows= []
headers= []
response=rq.get(url)
ifresponse.status_code==200:
soup=BeautifulSoup(response.text,'html.parser')
#print(soup)
table=soup.find('table',attrs={'class':'wikitable sortable'})
#print(table)
iflen(headers) ==0:
forthintable.find("tr").find_all("th"):
headers.append(th.text.strip())
headers.append('URL')
fortrintable.find_all("tr")[1:]:
cells= []
tds=tr.find_all("td")
fortdintds:
cells.append(td.text.strip())
iftd.find('a'):
link=td.find('a')['href']
#print(link)
cells=cells+ [link]
rows.append(cells)
df=pd.DataFrame(rows, columns=headers)
print(df.tail(20))