Скачать файл из запроса POST в scrapy - PullRequest
0 голосов
/ 16 февраля 2019

Я знаю, что есть встроенное промежуточное ПО для обработки загрузок.но он принимает только URL.но в моем случае моя ссылка для скачивания - это запрос POST.

Когда я сделал этот PDF-файл запроса POST, начинается загрузка.

Теперь я хочу загрузить этот файл из запроса POST в scrapy.

Веб-сайт http://scrb.bihar.gov.in/View_FIR.aspx Вы можете ввести район Aurangabad и полицейский участок Kasma PS

В последнем столбце status есть ссылка на скачивание файла.

        ps_x = '//*[@id="ctl00_ContentPlaceHolder1_ddlPoliceStation"]//option[.="Kasma PS"]/@value'
        police_station_val = response.xpath(ps_x).extract_first()
        d_x = '//*[@id="ctl00_ContentPlaceHolder1_ddlDistrict"]//option[.="Aurangabad"]/@value'
        district_val = response.xpath(d_x).extract_first()
        viewstate = response.xpath(self.viewstate_x).extract_first()
        viewstategen = response.xpath(self.viewstategen_x).extract_first()
        eventvalidator = response.xpath(self.eventvalidator_x).extract_first()
        eventtarget = response.xpath(self.eventtarget_x).extract_first()
        eventargs = response.xpath(self.eventargs_x).extract_first()
        lastfocus = response.xpath(self.lastfocus_x).extract_first()
        payload = {
            '__EVENTTARGET': eventtarget,
            '__EVENTARGUMENT': eventargs,
            '__LASTFOCUS': lastfocus,
            '__VIEWSTATE': viewstate,
            '__VIEWSTATEGENERATOR': viewstategen,
            '__EVENTVALIDATION': eventvalidator,
            'ctl00$ContentPlaceHolder1$ddlDistrict': district_val,
            'ctl00$ContentPlaceHolder1$ddlPoliceStation': police_station_val,
            'ctl00$ContentPlaceHolder1$optionsRadios': 'radioPetioner',
            'ctl00$ContentPlaceHolder1$txtSearchBy': '',
            'ctl00$ContentPlaceHolder1$rptItem$ctl06$lnkStatus.x': '21',
            'ctl00$ContentPlaceHolder1$rptItem$ctl06$lnkStatus.y': '24',
        }
        headers = {
            'Connection': 'keep-alive',
            'Cache-Control': 'max-age=0',
            'Origin': 'http://scrb.bihar.gov.in',
            'Upgrade-Insecure-Requests': '1',
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Referer': 'http://scrb.bihar.gov.in/View_FIR.aspx',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.9',
        }
        # req = requests.post(response.url, data=payload, headers=headers)
        # with open('pdf/ch.pdf', 'w+b') as f:
        #     f.write(req.content)

1 Ответ

0 голосов
/ 17 февраля 2019

Когда вы нажимаете donwload, веб-браузер отправляет запрос POST.enter image description here Таким образом, этот ответ, упомянутый El Ruso ранее , применим в вашем случае

.....
    def parse(self, response):
    ......
        yield scrapy.FormRequest("http://scrb.bihar.gov.in/View_FIR.aspx",.#your post request configuration, callback=self.save_pdf)

    def save_pdf(self, response):
        path = response.url.split('/')[-1]
        self.logger.info('Saving PDF %s', path)
        with open(path, 'wb') as f:
            f.write(response.body)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...