Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create spider for player transfers #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tfmkt/spiders/transfers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from tfmkt.spiders.common import BaseSpider
from scrapy.shell import inspect_response
import re
import json


class PlayerTransfersSpider(BaseSpider):
name = 'transfers'

def parse(self, response, parent):
"""Parse player's page to collect transfer history URL.

@url https://www.transfermarkt.co.uk/ayoze-perez/profil/spieler/246968
@returns requests 1 1
@cb_kwargs {"parent": "dummy"}
"""
player_id = response.url.split('/')[-1]
transfers_api_url = f"/ceapi/transferHistory/list/{player_id}"

cb_kwargs = {
'parent': parent
}

yield response.follow(transfers_api_url, self.parse_transfers, cb_kwargs=cb_kwargs)

def parse_transfers(self, response, parent):
"""Extract player's transfer history from API response.

@url https://www.transfermarkt.co.uk/ceapi/transferHistory/list/246968
@returns items 7 7
@cb_kwargs {"parent": {"type": "player", "href": "/ayoze-perez/profil/spieler/246968"}}
@scrapes type href parent season date from to market_value fee
"""

data = json.loads(response.text)

for transfer in data['transfers']:
yield {
'type': 'transfer',
'href': transfer['url'],
'parent': parent,
'season': transfer['season'],
'date': transfer['dateUnformatted'],
'from': {
'name': transfer['from']['clubName'],
'url': transfer['from']['href']
},
'to': {
'name': transfer['to']['clubName'],
'url': transfer['to']['href']
},
'market_value': transfer['marketValue'],
'fee': transfer['fee']
}