Post请求在Scrapy中不起作用。在邮递员工作。我哪里错了?

2024-06-08 04:41:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我用scrapy.FormRequest.from_response来发帖。我正在通过验证码,所有的数据都在适当的地方,但没有得到我期望的表。 But this message 这是我的有效载荷

{
  'captcha': '680336',
  'districtCode': '714',
  'sroVal': 'TADIKONDA(714)',
  'selectedSroId': 'null',
  'sroId': '7',
  'path': '/APCARDECClient',
  'regyear': '2018',
  'docSel': '1',
  'doct': '861'
}

以及我用于Post请求的邮件头。在

^{pr2}$

当我使用相同的数据来模拟Postman中的Post时,它工作得很好,并且我收到了预期的表。 Postman results 这是邮递员的有效载荷:

captcha:331683
districtCode:7
docSel:1
doct:861
path:/APCARDECClient
regyear:2018
selectedSroId:null
sroId:714
sroVal:TADIKONDA(714)

还有邮递员的头条:

Origin:http://registration.ap.gov.in
Upgrade-Insecure-Requests:1
DNT:1
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer:http://registration.ap.gov.in/APCARDECClient/FetchHelpDetails
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.9,ru;q=0.8,uk;q=0.7
Cookie:JSESSIONID=dQJTbqhpYdg1PMjJZkYT9bTyLLnMqV43zxj2sws1gZ2fQ8fJn0CQ!-392092779
Content-Type:application/x-www-form-urlencoded

请告诉我我哪里错了? 我看到的唯一可能是这条线:

Content-Type:application/x-www-form-urlencoded

我需要在scray中显式地执行smth来对有效负载进行这样的编码吗?在

当我使用裸体

yield scrapy.Request(url, method ="POST",  body=json.dumps(payload),headers=headers,  callback = self.details1)

我的起始页是空的。 enter image description here


Tags: 数据pathapplicationnullcaptchascrapyaccept有效载荷
2条回答

不要使用FormRequest-它只是注入一些头和一些主体快捷方式。如果要复制请求,只需使用barebonesRequest。在

import headers
headers = {
    # make sure Content Type is set
    'Content-Type': 'application/x-www-form-urlencoded',
    # some other headers
}
payload = """foobar"""
Request(
    url,
    headers=headers,
    method='POST',
    body=payload,
)

好吧。所以问题出在'Content-Type': 'application/x-www-form-urlencoded', 我一开始就注意到了,但没有再多加注意。太蠢了。 为了通过这个页面,有效负载应该是urlencoded,如Content_Type所示。 下面是如何做到的。在

import urllib

payload = {'captcha': '865944',
                 'sroVal': 'TADIKONDA(714)',
                 'sroId': '714',
                 'regyear': '2017', 
                 'doct': '1',
                 'districtCode': '7',
                 'path': '/APCARDECClient',
                 'selectedSroId': 'null',
                 'docSel': '1',
           }

payload = urllib.urlencode(payload)

yield scrapy.Request(url, method ="POST",  body=payload ,headers=headers,  
                     callback = self.details1)

相关问题 更多 >

    热门问题