小编woo*_*rdb的帖子

Python Scrapy:来自表单请求的 400 响应

我一直在尝试抓取网站https://fbschedules.com/new-england-patriots-schedule/

本网站使用隐藏表单向php文件提交ajax请求: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)
Run Code Online (Sandbox Code Playgroud)

任何在正确方向上的帮助表示赞赏! …

python ajax scrapy web

5
推荐指数
1
解决办法
2001
查看次数

标签 统计

ajax ×1

python ×1

scrapy ×1

web ×1