You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scrapy爬虫问题:红黄牌识别逻辑错误,请求技术支持

红黄牌识别逻辑错误排查求助

我重构了原有代码,使用Pandas简化PowerBI分析,但遇到如下问题:
访问指定URL时,尝试添加红黄牌(Targetes)逻辑判断,但实际存在红牌(Targeta Vermella)时,代码却将其识别为黄牌(Tarjeta Groga),识别逻辑存在问题,希望得到帮助排查错误。

代码如下:

import scrapy
import csv
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
import requests
import os

class fcf_statsSpider(scrapy.Spider):
    name = 'fcf_stats'
    start_urls = ["https://www.fcf.cat/calendari/2324/futbol-11/juvenil-primera-divisio/grup-2"]

    def parse(self, response):
        # Extract links from the tables using XPath selector
        links = response.xpath('//*[@class="calendaritablehover"]/a/@href').getall()

        for link in links:
            yield response.follow(link, callback=self.parse_match_data)

    def parse_match_data(self, response):
        
        # Extract the HTML content from the Scrapy response
        html_content = response.text
        
        self.temporada = response.css('.acta-info.print-acta-temp::text').get().strip()
        
        # Read all tables from the web page
        tables = pd.read_html(html_content)

        # # Fetch the HTML content of the URL
        # response = requests.get(response)
        # html_content = response.text

        # Create a BeautifulSoup object to parse the HTML
        soup = BeautifulSoup(html_content, 'html.parser')

        # Extract the elements with class "col-md-4 p-0_ml"
        elements_with_class = soup.find_all(class_="col-md-4 p-0_ml")

        # Check if 'Targetes' is present in elements_with_class[0]
        targetes_in_element_0 = 'Targetes' in elements_with_class[0].get_text()

        # Check if 'Targetes' is present in elements_with_class[2]
        targetes_in_element_2 = 'Targetes' in elements_with_class[2].get_text()

        # Extract the text for the first two elements with class "acta-equip"
        equip_elements = soup.find_all(class_="acta-equip")

        # Check if there are at least two elements with class "acta-equip"
        if len(equip_elements) >= 2:
            eq_local = equip_elements[0].get_text(strip=True)
            eq_visitant = equip_elements[1].get_text(strip=True)
        else:
            # Handle the case where there are not enough elements
            eq_local = None
            eq_visitant = None
            
        # Extract the text of the element with class "acta-info2 print-acta-jornada"
        acta_info = soup.find(class_="acta-info2 print-acta-jornada")
        acta_info_text = acta_info.get_text(strip=True) if acta_info else None

        # Define the column names to filter
        desired_column_names = ['Titulars', 'Suplents', 'Equip Tècnic', 'Targetes', 'Àrbitres', 'Gols', 'Estadi', 'Comparativa']

        # Flag to determine if we've found the first table in desired_column_names
        found_first_table = False

        # Create a dictionary to store tables with matching column names
        table_dict = {}

        # Flags to determine if we've found the first table for each desired column name
        found_first_table_dict = {col_name: False for col_name in desired_column_names}

        # Loop through the tables and append the matching ones to the dictionary
        for table in tables:
            matching_col_name = next((col_name for col_name in desired_column_names if col_name in table.columns), None)
            if matching_col_name:
                # Determine the "Equip" column values based on the conditions
                if matching_col_name == 'Targetes':
                    if targetes_in_element_0 and not found_first_table_dict[matching_col_name]:
                        equip_column_values = [eq_local] * len(table)
                    elif targetes_in_element_2 and not found_first_table_dict[matching_col_name]:
                        equip_column_values = [eq_visitant] * len(table)
                    else:
                        equip_column_values = ['Match_Stats'] * len(table)
                else:
                    equip_column_values = [eq_local if not found_first_table_dict[matching_col_name] else eq_visitant] * len(table)
                
                equip_column = pd.Series(equip_column_values, name='Equip', dtype='object')

                # Create a new DataFrame with the "ActaInfo" column
                acta_info_column = pd.Series([acta_info_text] * len(table), name='ActaInfo', dtype='object')

                # Concatenate the "Equip" and "ActaInfo" columns with the original table
                table = pd.concat([equip_column, acta_info_column, table], axis=1)

                # Update the flag after processing the first table for the current column name
                if not found_first_table_dict[matching_col_name]:
                    found_first_table_dict[matching_col_name] = True

                # Check if a table with the matching column name is already in the dictionary
                if matching_col_name in table_dict:
                    # If it exists, append the current table to it
                    table_dict[matching_col_name] = pd.concat([table_dict[matching_col_name], table], ignore_index=True)
                else:
                    # If it doesn't exist, create a new entry in the dictionary
                    table_dict[matching_col_name] = table
                    
                # Add the "Goal_Type" column to the "Gols" table and set values based on specific class values
                if matching_col_name == 'Gols' and 'Goal_Type' not in table.columns:
                    table['Goal_Type'] = None
                    
                    # Find elements with class values in the HTML content
                    gol_normal_elements = soup.find_all(class_="faf-pilota_base p-a stat-center gol-normal")
                    gol_penal_elements = soup.find_all(class_="faf-pilota_base p-a stat-center gol-penal")
                    gol_propia_elements = soup.find_all(class_="faf-pilota_base p-a stat-center gol-propia")
                    
                    # Set "Goal_Type" based on class values
                    for index, row in table.iterrows():
                        if gol_normal_elements and index < len(gol_normal_elements):
                            table.at[index, 'Goal_Type'] = 'Gol_Normal'
                        elif gol_penal_elements and index < len(gol_penal_elements):
                            table.at[index, 'Goal_Type'] = 'Gol_Penal'
                        elif gol_propia_elements and index < len(gol_propia_elements):
                            table.at[index, 'Goal_Type'] = 'Gol_Propia'
                
                # Add the "Booking" column to the "Targetes" table and set values based on specific class values
                if matching_col_name == 'Targetes' and 'Booking' not in table.columns:
                    table['Booking'] = None

                    # Find elements with class values in the HTML content
                    groga_s_elements = soup.find_all(class_="groga-s")
                    vermella_s_elements = soup.find_all(class_="vermella-s")

                    # Set "Booking" based on class values
                    for index, row in table.iterrows():
                        if groga_s_elements and index < len(groga_s_elements):
                            table.at[index, 'Booking'] = 'Tarjeta Groga'
                        elif vermella_s_elements and index < len(vermella_s_elements):
                            table.at[index, 'Booking'] = 'Targeta Vermella'
                            
        # Save the tables in the dictionary to separate CSV files for each column name
        for col_name, table in table_dict.items():
            csv_filename = f'{col_name}.csv'
            if os.path.exists(csv_filename):
                # If the CSV file already exists, read the existing data
                existing_data = pd.read_csv(csv_filename)
                # Concatenate the existing data with the new data
                table = pd.concat([existing_data, table], ignore_index=True)
            
            # Save the concatenated data (including existing data) back to the same CSV file
            table.to_csv(csv_filename, index=False)

内容的提问来源于stack exchange,提问作者Avaya Tech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 00:04:53