Scrapy Xpath返回null,但在Chrome中工作正常

2024-05-28 23:20:09 发布

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

我对Scrapy很陌生,但我正在设计一个web scrape来从GoFundMe中获取某些信息,特别是在本例中,是指为某个项目捐款的人数。我编写了一个xpath语句,它在Chrome中运行良好,但在Scrapy中返回null

一个随机的例子是https://www.gofundme.com/f/passage/donations,目前有22项捐款。在Chrome inspect中输入以下内容时,我需要“捐款(22)”:

//h2[@class="heading-5 mb0"]/text()

然而,在我的痒蜘蛛中,以下结果为空-

class DonationsSpider(scrapy.Spider):
name = 'get_donations'

start_urls = [
    'https://www.gofundme.com/f/passage/donations'
]

def parse(self, response):
    amount_of_donations = response.xpath('//h2[@class="heading-5 mb0"]/text()').extract_first()

    yield{
        'Donations': amount_of_donations
    }

有人知道为什么Scrapy看不到这个值吗

我这样做是为了找出爬行器的其余部分需要循环多少次,因为当我硬编码这个值时,它可以毫无问题地工作,并产生所有的捐款


Tags: texthttpscomresponsewwwh2chromexpath
1条回答
网友
1楼 · 发布于 2024-05-28 23:20:09

好吧,因为有许多请求正在进行中,请满足请求“https://www.gofundme.com/f/passage/donations”。在哪里

your chrome is smart enough to under stand javascript, using that smartness it reads the JavaScript code and fetches all the responses from different different endpoints to fulfil your request

有一个对端点“https://gateway.gofundme.com/web-gateway/v1/feed/passage/counts”的请求,该端点加载您正在查找的数据。这是python脚本无法做到的,也不推荐使用

相反,您可以直接调用该api并获得数据,好消息是端点响应非常结构化、易于解析的JSON数据

我相信您也在寻找来自这个端点“https://gateway.gofundme.com/web-gateway/v1/feed/passage/donations?limit=20&offset=0&sort=recent”的数据

有关更多信息,请参阅我的博客clicking here

相关问题 更多 >

    热门问题