【问题标题】:Python Scrapy subscript-after returns only the first resultPython Scrapy subscript-after 只返回第一个结果
【发布时间】:2020-08-03 00:42:14
【问题描述】:

我无法确定为什么我的蜘蛛只在 'match_id' 使用下标后返回第一个结果。

这是有问题的代码: match_id = response.xpath('substring-after(//tr/td[13]/a/@href, "/match-detail/?id=")').extract( )

我的蜘蛛:

import scrapy
import datetime
from datetime import timedelta

class Tennis_ExplorerSpider(scrapy.Spider):
    name = 'tennis_explorer'
    allowed_domains = ['tennisexplorer.com']

    def daterange(start_date, end_date):
        for n in range(int((end_date - start_date).days)):
            yield start_date + timedelta(n)
    
    start_date = datetime.datetime.today() - datetime.timedelta(days=1)
    end_date = datetime.datetime.today() + datetime.timedelta(days=1)    
    start_urls = []
    start_url='https://www.tennisexplorer.com/matches/?type=all&year='
    for single_date in daterange(start_date, end_date):
        start_urls.append(single_date.strftime(start_url+"%Y&month=%m&day=%d&timezone=-6"))


   
    def parse(self, response):
            #self.logger.debug('callback "parse": got response %r' % response)
            #Extracting the content using xpath
            time = response.xpath('//tr/td[1][@class="first time"]/text()').extract()
            match_id = response.xpath('substring-after(//tr/td[13]/a/@href, "/match-detail/?id=")').extract()
            player1 = response.xpath('//tr[not(contains(@class, "head"))]/td[2][@class="t-name"]/a[contains(@href, "/player/")]/text()').extract()
            player2 = response.xpath('//tr[not(contains(@class, "head"))]/td[1][@class="t-name"]/a[contains(@href, "/player/")]/text()').extract()
            player1_sets = response.xpath('//tr/td[3][@class="result"]/text()').extract()
            player2_sets = response.xpath('//tr/td[2][@class="result"]/text()').extract()

            
            #Give the extracted content row wise
            for item in zip(time, match_id, player1, player2, player1_sets, player2_sets):
                #create a dictionary to store the scraped info
                scraped_info = {
                    'time' : item[0],                    
                    'match_id' : item[1],
                    'player1' : item[2],
                    'player2' : item[3],                    
                    'player1_sets' : item[4],
                    'player2_sets' : item[5]
                }
    
                #yield or give the scraped info to scrapy
                yield scraped_info
        

            

非常感谢 Ant 的帮助。

【问题讨论】:

    标签: python python-3.x xpath web-scraping scrapy


    【解决方案1】:

    您的 XPath 没有选择任何内容。应该是td[12] 而不是td[13]

    substring-after(//tr/td[13]/a/@href, "/match-detail/?id=")
    

    最好使用相对的:

    //table[@class="result"]//a[contains(@href,"match-detail")]/@href
    

    但这不是问题所在。 在 XPath 1.0 中,substring 函数只会在 XPath 返回多个节点时输出应用于第一个节点的函数的结果。

    使用Python 生成列表。获取值,然后用re.sub 替换无用的部分。将此行添加到您的代码中以生成 id 列表:

    import re
    data = response.xpath('//table[@class="result"]//a[contains(@href,"match-detail")]/@href').extract()
    ids=[re.sub('^.+=','',el) for el in data]
    

    page 的输出:262 个元素。

    ['789669', '779307', '786865', '789668', '786866',..., '1892793', '1892795', '1892802', '1892794', '1892955']
    

    【讨论】:

    • 非常感谢!我已经为此苦苦挣扎了好几个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多