Estoy tratando de usar Scrapy para devolver los resultados y las estadísticas de los juegos en vivo en SofaScore.
Sitio: https://www.sofascore.com/
El código a continuación:
import scrapy class SofascoreSpider(scrapy.Spider): name = 'SofaScore' allowed_domains = ['sofascore.com'] start_urls = ['http://sofascore.com/'] def parse(self, response): time1 = response.xpath("/html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div/div").extract() print(time1) pass
Traté de usar response.xpath("//html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/div/div/div/div/div[2]/a/div/div").getall()
también, pero no devuelve nada. Usé tantos xpath diferentes y no volvió. ¿Qué estoy haciendo mal?
Me gusta, hoy 10/06 el primer partido en la página es Francia vs Austria, xpath: /html/body/div[1]/main/div/div[2]/div/div[3]/div[2]/ div/div/div/div/div[2]/a/div/div
Los datos se generan con JavaScript, pero puede obtenerlos de la API.
Abra devtools en el navegador y haga clic en la pestaña de network
. Luego haga clic en el botón en live
y mire de dónde carga los datos. Luego mire el archivo JSON para ver su estructura.
import scrapy class SofascoreSpider(scrapy.Spider): name = 'SofaScore' allowed_domains = ['sofascore.com'] start_urls = ['https://api.sofascore.com/api/v1/sport/football/events/live'] custom_settings = {'DOWNLOAD_DELAY': 0.4} def start_requests(self): headers = { "Accept": "*/*", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en-US,en;q=0.5", "Cache-Control": "no-cache", "Connection": "keep-alive", "DNT": "1", "Host": "api.sofascore.com", "Origin": "https://www.sofascore.com", "Pragma": "no-cache", "Referer": "https://www.sofascore.com/", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-site", "Sec-GPC": "1", "TE": "trailers", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" } yield scrapy.Request(url=self.start_urls[0], headers=headers) def parse(self, response): events = response.json() events = events['events'] # now iterate throught the list and get what you want from it # example: for event in events: yield { 'event name': event['tournament']['name'], 'time': event['time'] }