【发布时间】: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