- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Read HTMLpy.py
More file actions
Latest commit
40 lines (32 loc) · 1.22 KB
/
Copy pathPython Read HTMLpy.py
File metadata and controls
40 lines (32 loc) · 1.22 KB
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
34
35
36
37
38
39
40
importrequestsasrq
frombs4importBeautifulSoup
importpandasaspd
url='https://en.wikipedia.org/wiki/List_of_National_Football_League_career_scoring_leaders'
amount_of_pages=1#5194
rows= []
headers= []
foriinrange(1,amount_of_pages+1): #<-- if theres 4796 pages, your range needs to be to 4797. range goes from (start, end) but the is not inclusive of the end value
response=rq.get(url)
print (i)
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))