什么是Scrapy返回相同的项目,而不是所有的项目在上市?

bt1cpqcv  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(146)

我已经创建了一个蜘蛛,它的目的是让我一个历史的看法乐透号码。脚本是工作,但它是返回第一个项目,然后复制它。
这是我的代码,它返回第一行,然后重复它的次数,有一行,但它应该返回每一行。

import scrapy

class LottoSpider(scrapy.Spider):
    name = 'lottos'
    start_urls = [
        'https://www.lotteryextreme.com/powerball/results'
    ]

    def parse(self, response):

        for results in response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "wyn", " " ))]'):

                date = response.css('span.f20::text').get()
                number1 = response.css('.results2 td::text')[0].extract()
                number2 = response.css('.results2 td::text')[1].extract()
                number3 = response.css('.results2 td::text')[2].extract()
                number4 = response.css('.results2 td::text')[3].extract()
                number5 = response.css('.results2 td::text')[4].extract()
                powerball = response.css('.results2 td::text')[6].extract()

                yield {

                    'date': date,
                    'number1': number1,
                    'number2': number2,
                    'number3': number3,
                    'number4': number4,
                    'number5': number5,
                    'powerball': powerball,

                }
xzabzqsa

xzabzqsa1#

在for循环中,需要使用results而不是response

import scrapy

class LottoSpider(scrapy.Spider):
    name = 'lottos'
    start_urls = ['https://www.lotteryextreme.com/powerball/results']

    def parse(self, response):
        for results in response.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "wyn", " " ))]'):
            date = results.css('span.f20::text').get()
            number1 = results.css('.results2 td::text')[0].get()
            number2 = results.css('.results2 td::text')[1].get()
            number3 = results.css('.results2 td::text')[2].get()
            number4 = results.css('.results2 td::text')[3].get()
            number5 = results.css('.results2 td::text')[4].get()
            powerball = results.css('.results2 td::text')[6].get()

            yield {
                'date': date,
                'number1': number1,
                'number2': number2,
                'number3': number3,
                'number4': number4,
                'number5': number5,
                'powerball': powerball,
            }

相关问题