如何将数据拆分成多行,并根据cod中定义的标题进行排列

2024-04-28 09:17:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python Scrapy来刮网站如果这是一个noob问题,我很抱歉,因为我是这方面的新手,我知道我们可以用.split()分割数据,但我不知道在哪里使用它。你知道吗

这是我的蜘蛛

import scrapy
from scrapy.loader import ItemLoader
from ..items import FreelancerItem

class MatchSpider(scrapy.Spider):
    name = 'match'
    start_urls = ['http://www.totalcorner.com/match/schedule/20191215/']

    def parse(self, response):
        items = FreelancerItem()
        all_table_cols = response.xpath("//tbody[@class='tbody_match']")
        for col in all_table_cols:
            League=  col.css(".td_league a::text").extract()
            Time= col.css(".match_status_minutes::text").extract()
            Match_status = col.xpath("//span[@class='match_status_minutes']/text").extract()
            Yellow_home = col.css(".yellow_card::text").extract()
            Red_home = col.css(".match_home .red_card::text").extract()
            Home = col.css(".match_home a span::text").extract()
            Score=col.css(".match_goal::text").extract()
            Away = col.css(".match_away a span::text").extract()
            Yellow_away = col.css(".match_away .yellow_card::text").extract()
            Red_away = col.css(".match_away .red_card::text").extract()
            Handicap = col.css(".match_handicap::text").extract()
            Corner= col.css(".match_corner::text").extract()
            Goal_Line=col.css(".total_goals div::text").extract()
            Tips=col.css(".newlabel::text").extract()
            Dangerous_attack= col.css(".match_attach div::text").extract()
            Shots=col.css(".match_shoot div::text").extract()


            items["League"] =League
            items["Time"] = Time
            items["Match_status"] = Match_status
            items["Yellow_home"] = Yellow_home
            items["Red_home"] = Red_home
            items["Home"] = Home
            items["Score"] = Score
            items["Away"] = Away
            items["Yellow_away"] = Yellow_away
            items["Red_away"] = Red_away
            items["Handicap"] = Handicap
            items["Corner"] = Corner
            items["Goal_Line"] = Goal_Line
            items["Tips"] = Tips
            items["Dangerous_attack"] = Dangerous_attack
            items["Shots"] = Shots

            yield items

这是我的items.py

import scrapy

class FreelancerItem(scrapy.Item):
    League=scrapy.Field()
    Time = scrapy.Field()
    Match_status = scrapy.Field()
    Yellow_home = scrapy.Field()
    Red_home = scrapy.Field()
    Home = scrapy.Field()
    Score = scrapy.Field()
    Away = scrapy.Field()
    Yellow_away = scrapy.Field()
    Red_away = scrapy.Field()
    Handicap = scrapy.Field()
    Corner = scrapy.Field()
    Goal_Line = scrapy.Field()
    Tips = scrapy.Field()
    Dangerous_attack = scrapy.Field()
    Shots = scrapy.Field()

我也想安排它根据标题,但在csv输出,它安排标题按字母顺序。你知道吗

请帮助我学习

谢谢。你知道吗


Tags: textimportfieldhomematchstatusextractitems