首页 > 解决方案 > 单击 Scrapy-Splash 中的显示按钮

问题描述

我正在使用 scrapy-splash 抓取以下网页,http://www.starcitygames.com/buylist/,我必须登录才能获取我需要的数据。这很好,但是为了获取我需要单击显示按钮的数据,以便我可以抓取该数据,在单击按钮之前无法访问我需要的数据。我已经得到了一个答案,告诉我我不能简单地单击显示按钮并抓取显示的数据,我需要抓取与该信息关联的 JSON 网页,但我担心抓取 JSON 会变成红色标记给网站的所有者,因为大多数人不会打开 JSON 数据页面,人类需要几分钟才能找到它,而计算机会更快。所以我想我的问题是,无论如何要刮掉我点击显示的网页并从那里开始,还是我别无选择,只能刮掉 JSON 页面?这是我到目前为止所得到的......

import scrapy
from ..items import NameItem

class LoginSpider(scrapy.Spider):
    name = "LoginSpider"
    start_urls = ["http://www.starcitygames.com/buylist/"]

    def parse(self, response):
        return scrapy.FormRequest.from_response(
        response,
        formcss='#existing_users form',
        formdata={'ex_usr_email': 'abc@example.com', 'ex_usr_pass': 'password'},
        callback=self.after_login
        )



    def after_login(self, response):
        item = NameItem()
        display_button = response.xpath('//a[contains(., "Display>>")]/@href').get()

        yield response.follow(display_button, self.parse)

        item["Name"] = response.css("div.bl-result-title::text").get()
        return item

网站截图 HTML 代码

标签: pythonweb-scrapingscrapysplash-screenscrapy-splash

解决方案


您可以使用浏览器的开发人员工具来跟踪该点击事件的请求,该请求采用良好的 JSON 格式,也不需要 cookie(登录):

http://www.starcitygames.com/buylist/search?search-type=category&id=5061

唯一需要填写的是category_id与此请求相关的内容,可以从 HTML 中提取并在您的代码中声明。

分类名称:

//*[@id="bl-category-options"]/option/text()

类别编号:

//*[@id="bl-category-options"]/option/@value

使用 JSON 比解析 HTML 简单得多。


推荐阅读