Created
January 17, 2018 23:33
-
-
Save Netzvamp/7402f9f3bd475d0d0b8d30501d8e4f27 to your computer and use it in GitHub Desktop.
Fussball.de Ergebnis Crawler
This file contains 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
from requests import get | |
from bs4 import BeautifulSoup | |
import re | |
import json | |
team_id = '011MIBM9IK000000VTVG0001VTR8C1K7' | |
url = "http://www.fussball.de/ajax.team.prev.games/-/team-id/{}".format(team_id) | |
response = get(url) # Download team games page | |
team_soup = BeautifulSoup(response.content, "lxml") # Parse | |
game_links = team_soup.find_all('a', href=re.compile("http://www.fussball.de/spiel")) # finde links zu spielen | |
for game_link in game_links: | |
if 'Zum Spiel' in game_link.contents[1]: # Doppelte Links ignorieren | |
game_response = get(game_link['href']) # Spielseite runterladen | |
game_soup = BeautifulSoup(game_response.content, "lxml") | |
game_json_tag = game_soup.find('div', attrs={'data-match-events': True}) # finde json in 'data-match-events' | |
game_json = json.loads(game_json_tag['data-match-events'].replace('\'', '\"')) # string zu python dict | |
home_team = game_soup.select('.info-home .club-name')[0].contents[0] | |
away_team = game_soup.select('.info-away .club-name')[0].contents[0] | |
home_team_score = 0 | |
away_team_score = 0 | |
for (root_key, root_value) in game_json.items(): | |
if isinstance(root_value, dict) and 'events' in root_value: # Wenn wir den Events Zweig haben | |
for event in root_value['events']: | |
if event['type'] == 'goal': # Wir haben ein Tor | |
if event['team'] == 'home': # Das Heimteam hat das Tor geschossen | |
home_team_score += 1 | |
if event['team'] == 'away': # Das Auswärtsteam hat ein Tor geschossen | |
away_team_score += 1 | |
print( | |
"Spiel:", | |
home_team, "gegen", away_team, " - ", | |
home_team_score, ":", away_team_score, " URL:", game_link['href'] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment