-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebscrapping table 2 espn exemplo variavel para link.py
37 lines (35 loc) · 1.88 KB
/
webscrapping table 2 espn exemplo variavel para link.py
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
import requests
from bs4 import BeautifulSoup
from pathlib import Path
import time
#nao vai funcionar, pois o url nao existe, esse um exemplo onde o final da url muda, quando tem next na pagina para continuar vendo a tabela, o url que funciona somente tem um link showmore
# uma barra invertida siginifca que o codigo continua na proxima linha
num = 1
caminho = Path(r"C:\Users\RONALDOAPARECIDODASI\Documents\MeusProjetos\Treinamento-Python\Beatiful_Soup\basketball_stats table 2 exemplo.txt")
url = 'https://www.espn.com/nba/statistics/player/_/stat/assists/sort/avgAssists/count/{}'.format(num)
#headers= {'User-Agent': 'Mozilla/5.0'}
#headers= {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0'}
#response = requests.get(url, headers = headers)
with open(caminho, 'w') as r:
r.write('BASKETBALL ASSIST TABLE\n')
while num <272:
url = 'https://www.espn.com/nba/statistics/player/_/stat/assists/sort/avgAssists/count/{}'.format(num)
time.sleep(1) # para dar tempo de puxa, para caso o servidor esta muito busy
response = requests.get(url)
if response.status_code == 200: #200 e codigo para pagina que carrega, outro
soup = BeautifulSoup(response.content, "html.parser")
stat_table = soup.find_all('table', class_ = 'Table Table--align-right Table--fixed Table--fixed-left')
if len(stat_table) < 2: #para cabo tenha muita tabela na soup do html
stat_table = stat_table[0]
with open(caminho, 'a') as r: #a para appending
for row in stat_table.find_all('tr'):
for cell in row.find_all('td'):
r.write(cell.text.ljust(22))
r.write('\n')
else:
print('Too many tables')
else:
print('No response')
print(num)
#no site de treinamento o site mostra 40 linhas de estatistica por vez
num += 40