[英]Click Button in Scrapy-Splash
我正在编写一个scrapy-splash程序,我需要点击网页上的显示按钮,如下图所示,以显示第10版的数据,所以我可以抓取它。 我有我在下面尝试过的代码,但它不起作用。 我需要的信息只有在我单击显示按钮时才能访问。 更新:仍在为此苦苦挣扎,我必须相信有办法做到这一点。 我不想抓取 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': 'email123@example.com', 'ex_usr_pass': 'password123'},
callback=self.after_login
)
def after_login(self, response):
item = NameItem()
display_button= response.xpath('//a[contains(., "- Display>>")]/@href').get()
response.follow(display_button, self.parse)
item["Name"] = response.css("div.bl-result-title::text").get()
return item
您的代码无法工作,因为没有锚元素和 href 属性。 单击该按钮将向http://www.starcitygames.com/buylist/search?search-type=category&id=5061
发送一个XMLHttpRequest
,您想要的数据将在 JSON 响应中找到。
Display
。Headers
选项卡中,您将找到请求 URL,在Preview
或Response
选项卡中,您可以检查 JSON。id
来构建请求 URL。 您可以通过解析使用此 XPath 找到的script
元素来找到它//script[contains(., "categories")]
http://www.starcitygames.com/buylist/search?search-type=category&id=5061
并获取你想要的数据。$ curl 'http://www.starcitygames.com/buylist/search?search-type=category&id=5061'
{"ok":true,"search":"10th Edition","results":[[{"id":"46269","name":"Abundance","subtitle":null,"condition":"NM\/M","foil":true,"is_parent":false,"language":"English","price":"20.000","rarity":"Rare","image":"cardscans\/MTG\/10E\/en\/foil\/Abundance.jpg"},{"id":"176986","name":"Abundance","subtitle":null,"condition":"PL","foil":true,"is_parent":false,"language":"English","price":"12.000","rarity":"Rare","image":"cardscans\/MTG\/10E\/en\/foil\/Abundance.jpg"}....
如您所见,您甚至不需要登录网站或Splash
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.