Python 如何使用Scrapy为该表编写.CSS提取代码

Python 如何使用Scrapy为该表编写.CSS提取代码,python,scrapy,Python,Scrapy,我在Windows Vista 64位上使用Python.org 2.7 64位版本。我正在使用Scrapy构建一个递归webscraper。以下是一个for循环,用于从链接在其下方的页面上的表中提取数据: rows = sel.xpath('//table[@id="player-fixture"]//tbody//tr') for row in rows: print 'date:', "".join( row.css('.date::text')

我在Windows Vista 64位上使用Python.org 2.7 64位版本。我正在使用Scrapy构建一个递归webscraper。以下是一个for循环,用于从链接在其下方的页面上的表中提取数据:

rows = sel.xpath('//table[@id="player-fixture"]//tbody//tr')

        for row in rows:

            print 'date:', "".join( row.css('.date::text').extract() ).strip()
            print 'result:', "".join( row.css('.result a::text').extract() ).strip()
            print 'team_home:', "".join( row.css('.team.home a::text').extract() ).strip()
            print 'team_away:', "".join( row.css('.team.away a::text').extract() ).strip()
            print 'info:', "".join( row.css('.info::text').extract() ).strip(), "".join( row.css('.info::attr(title)').extract() ).strip()
            print 'rating:', "".join( row.css('.rating::text').extract() ).strip()
            print 'incidents:', ", ".join( row.css('.incidents-icon::attr(title)').extract() ).strip()
            print '-'*40

我现在正试图在站点的另一部分解析来自不同格式表的数据,但我正在努力解决如何为它编写.CSS代码。可在此处找到该表:

此表第一个选项卡的HTML如下所示:

<table id="team-squad-stats-summary-grid" class="grid with-centered-columns hover">
            <thead>
                <tr>
                    <th class="sortable rank" data-property="Rank" data-default-sort-dir="asc" title="Rank">R</th>
                    <th class="sortable rgn" data-property="PlayerRegionCode" data-default-sort-dir="asc" title="Country"></th>
                    <th class="sortable pn" data-property="PlayerName" data-default-sort-dir="asc" title="Player Name">Name</th>
                    <th class="sortable pos" data-property="RealPosition" data-default-sort-dir="asc" title="Position">Pos</th>
                    <th class="sortable age" data-property="Age" data-default-sort-dir="asc" title="Age">Age</th>
                    <th class="sortable hg" data-property="Height" title="Height">cm</th>
                    <th class="sortable wg" data-property="Weight" title="Weight">kg</th>

                    <th class="sortable ap" data-property="GameStarted"  title="First Eleven (Substitute)">Apps</th>
                    <th class="sortable g" data-property="Goals" title="Goals">Goal</th>
                    <th class="in-squad-detailed-view  sortable a" data-property="Assists" title="Assists">A</th>
                    <th class="sortable y" data-property="Yellow" title="Yellow Cards">Yel</th>
                    <th class="sortable r" data-property="Red" title="Red Cards">Red</th>

                    <th class="in-squad-detailed-view sortable spg" data-property="TotalShots" title="Shots per Game">SpG</th>
                    <th class="in-squad-detailed-view sortable ps" data-property="PassSuccess" title="Pass success percentage">PS%</th>
                    <th class="in-squad-detailed-view sortable aw" data-property="AerialWon" title="Aerial duels won per game">AW</th>
                    <th class="in-squad-detailed-view sortable mom" data-property="ManOfTheMatch" title="Man of the Match">MoM</th>
                    <th class="in-squad-detailed-view sortable rating" data-property="Rating" title="Average Rating">Rt</th>    

                </tr>
            </thead>
            <tbody id="team-squad-stats-summary-content"></tbody>
            <tfoot>
                <tr>
                    <td colspan="99" class="info">*Players shaded are players who are not currently active in team. (Loaned, sold, etc..)</td>
                </tr>
            </tfoot>
        </table>
        </div>
有人能帮忙吗


谢谢

如果我是你,我会在第808行解析数据存储(正如你在问题评论中提到的)。这已经是JSON格式。所以,对我来说,这里真的不需要刮毛。下面是我要做的:

  • 以您喜欢的任何方式获取页面的源代码。由于某些原因,我无法使用获取源代码。这可能与你的问题评论中提到的JS有关。然而,我能够在shell中发出下面的命令并接收页面的源代码。所以,使用python调用它,或者使用类似的东西

    卷曲

  • 查找文档中
    DataStore.prime('stage-player-stat',defaultteamplayersstatsconfigparams.defaultParams,
    的第一个索引和
    的下一个索引之间的文本。让我们调用这个字符串
    data

  • 将提取的字符串
    数据
    加载到python字典数组中:

    jdata=json.load(数据)


  • 现在我相信
    jdata
    将有一个字典数组,其中每个字典对应一个播放器。您可以使用这些字典中的每一个来访问您在原始问题中查找的玩家的统计信息。

    运行第二段代码时会发生什么?您确定第3行中有数据吗?@pgorsira没有使用此逻辑打印输出。行S3解析为与第一个示例中使用的表不同的表。您使用哪个选择器填充行S3?@pgorsira'rows3=sel.xpath('//table[@id=“top team stats summary grid”]//tbody//tr')您不应该查找“team-band stats summary grid”的id吗?
    for row in rows3:      
    
            rank = "".join( row.css('.rank::text').extract() ).strip() + ','
            playerregioncode = "".join( row.css('.playerregioncode a::text').extract() ).strip() + ','
            playername = "".join( row.css('.name::text').extract() ).strip() + ','
            realposition = "".join( row.css('.realposition::text').extract() ).strip() + ','
            age = "".join( row.css('.age:text').extract() ).strip() + ','
            height = "".join( row.css('.height::text').extract() ).strip() + ','