【发布时间】:2018-09-02 20:27:20
【问题描述】:
我一直在尝试抓取网站https://fbschedules.com/new-england-patriots-schedule/
本网站使用隐藏表单提交ajax请求到php文件:https://fbschedules.com/wp-admin/admin-ajax.php
在尝试模拟 AJAX 请求后,scrapy 对此代码返回 400 响应:
def parse(self, response):
headers = {
'User_Agent': user_agent,
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://fbschedules.com/new-england-patriots-schedule/',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Cookie': cookie,
'DNT': '1',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
}
data = {
'action': 'load_fbschedules_ajax',
'type': 'NFL',
'display': 'Season',
'team': 'New+England+Patriots',
'current_season': '2018',
'view': '',
'conference': '',
'conference-division': '',
'ncaa-subdivision': '',
'ispreseason': '',
'schedule-week': '',
}
yield scrapy.FormRequest.from_response('https://fbschedules.com/wp-admin/admin-ajax.php',
headers=headers,
formdata=data,
method='POST',
callback=self.schedule_parse)
对正确方向的任何帮助表示赞赏!
编辑:我还应该提到,我正在将这个蜘蛛作为单个脚本运行:
def start():
configure_logging()
runner = CrawlerRunner()
runner.crawl(NflSpider)
d = runner.join()
d.addBoth(lambda _: reactor.stop())
reactor.run()
开始抓取页面。 控制台输出如下:
2018-09-02 18:20:33 [scrapy.core.engine] 信息:蜘蛛打开
2018-09-02 18:20:33 [scrapy.extensions.logstats] 信息:已爬取 0 页 (以 0 页/分钟),抓取 0 项(以 0 项/分钟)
2018-09-02 18:20:33 [scrapy.extensions.telnet] 调试:Telnet 控制台 在 127.0.0.1:6024 上收听
2018-09-02 18:20:33 [scrapy.core.engine] 调试:已爬网 (400) https://fbschedules.com/wp-admin/admin-ajax.php>(引用者:无)
2018-09-02 18:20:33 [scrapy.spidermiddlewares.httperror] 信息: 忽略响应 https://fbschedules.com/wp-admin/admin-ajax.php>:HTTP 状态码是 未处理或不允许
2018-09-02 18:20:33 [scrapy.core.engine] 信息:关闭蜘蛛 (完)
【问题讨论】:
-
你不应该在那里使用
.from_response方法。请改用yield scrapy.FormRequest(...)。 -
我已将请求更新为 FormRequest。问题依旧......
-
响应正文中有返回
400状态码的消息吗?这些信息会很有帮助。 -
@dethos 你能告诉我如何从响应正文中获取信息吗?
-
您可以在this section of the documentation上查看有关如何访问请求错误正文的示例