Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Scrapy: 400 response from Form Request

I have been trying to scrape the website https://fbschedules.com/new-england-patriots-schedule/

This website uses a hidden form to submit an ajax request to the php file: https://fbschedules.com/wp-admin/admin-ajax.php

After attempting to simulate the AJAX request, scrapy returns a 400 response for this code:

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)

Any help in the right direction is appreciated!

Edit: I should also mention that I'm running this spider as a single script using:

def start():
    configure_logging()
    runner = CrawlerRunner()
    runner.crawl(NflSpider)
    d = runner.join()
    d.addBoth(lambda _: reactor.stop())

    reactor.run()

to start the crawling of the page. Console output is as follows:

2018-09-02 18:20:33 [scrapy.core.engine] INFO: Spider opened

2018-09-02 18:20:33 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)

2018-09-02 18:20:33 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024

2018-09-02 18:20:33 [scrapy.core.engine] DEBUG: Crawled (400) https://fbschedules.com/wp-admin/admin-ajax.php> (referer: None)

2018-09-02 18:20:33 [scrapy.spidermiddlewares.httperror] INFO: Ignoring response <400 https://fbschedules.com/wp-admin/admin-ajax.php>: HTTP status code is not handled or not allowed

2018-09-02 18:20:33 [scrapy.core.engine] INFO: Closing spider (finished)

like image 954
woodfordb Avatar asked Nov 08 '22 02:11

woodfordb


1 Answers

I had the same issue, I handled it by adding meta argument to the FormRequest parameters.

Try using scrapy.FormRequest instead of scrapy.FormRequest.from_response :

meta = {'handle_httpstatus_all': True}
yield FormRequest('https://fbschedules.com/wp-admin/admin-ajax.php',
                                           headers=headers,
                                           formdata=data,
                                           method='POST',
                                           meta=meta,
                                           callback=self.schedule_parse)
like image 110
Th3FreeSpirit Avatar answered Nov 14 '22 22:11

Th3FreeSpirit