javascript - scrapy-splash 渲染多于第一页

标签 javascript scrapy splash-js-render

我正在尝试抓取一个网站,但需要在所有页面中使用启动画面,因为它们的内容是动态创建的。 现在它只呈现第一页,而不是内容页或分页页。

代码如下:

import scrapy
from scrapy_splash import SplashRequest
import scrapy_splash

class ShutSpider(scrapy.Spider):
    name = 'Shut'
    def start_requests(self):
            yield SplashRequest(url='ROOTURL',callback=self.parse)

    def parse(self, response):
        # follow links to author pages
        content=response.xpath('//*[@id="iconQuesBar"]/a[4]/@href').extract()
        for href in content:
            yield response.follow(href.replace('?id=', ''), self.parse_QNA)
        if content == []:
            return
        # follow pagination links
        for href in response.xpath('//*[@id="body-div"]/table/tbody/tr[2]/td[3]/center/form/span/a/@href').extract():
            yield response.follow(href, self.parse)

    def parse_QNA(self, response):
        yield {
            'url': response.url,
            'title': response.xpath('//h1[@class = "head"]/text()').extract()

我试过所有我能想到的方法,但没有用。 我现在能想到的唯一解决方案是使用渲染 API 将链接发送到内容页面和分页,但我认为这是非常糟糕的编码,必须有另一种方法。

谢谢你的帮助。

最佳答案

代替 response.follow(),明确地为后续页面生成新的 SplashRequest。此外,在这种情况下,您还必须使用 response.urljoin()。这是修改后的代码:

import scrapy
from scrapy_splash import SplashRequest
import scrapy_splash

class ShutSpider(scrapy.Spider):
    name = 'Shut'
    def start_requests(self):
        yield SplashRequest(url='ROOTURL',callback=self.parse)

    def parse(self, response):
        # follow links to author pages
        content = response.xpath('//*[@id="iconQuesBar"]/a[4]/@href').extract()
        for href in content:
            yield SplashRequest(response.urljoin(href.replace('?id=', '')), self.parse_QNA)
        if content == []:
            return
        # follow pagination links
        for href in response.xpath('//*[@id="body-div"]/table/tbody/tr[2]/td[3]/center/form/span/a/@href').extract():
            yield SplashRequest(response.urljoin(href), self.parse)

    def parse_QNA(self, response):
        yield {
            'url': response.url,
            'title': response.xpath('//h1[@class = "head"]/text()').extract()

关于javascript - scrapy-splash 渲染多于第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47831446/

相关文章:

JavaScript : How to do Error Handling in Lexer generated by antlr?

javascript - 在 Firefox 中连续使用 Notifications API 的多个通知

javascript - Django:Ajax 没有从服务器响应接收数据

javascript - 点击java元素scrapy+selenium

python - Scrapy处理asp.net站点中的 session cookie或302

javascript - 在 angularjs 中在父页面和弹出页面之间传递数据的最佳方法

python - 如何使用装饰器使蜘蛛能够区分 scrapy 管道

docker - 谷歌应用引擎 : Load another Docker Image for Scrapy + Splash

python - Scrapy 不获取 response.css 上的标记

docker - 如何使用 docker 工具箱运行 splash