scrapy携带payload发送post请求

url_rl = "https://www.yijiupi.com/v31/Product/ListProduct"
                payload = '{"currentPage":1,"data":{"sonCategoryId":"%s","categoryIds":["%s"],"saleModel":-1,"sort":0,"currentPage":1,"pageSize":60},"pageSize":60,"addressId":1745761,"deviceType":3}'%(cat_id,cat_id)
                # return [scrapy.Request(url=url_rl, body=json.dumps(payload), method="POST", callback=self.get_page_num)]
                #这里的payload之前是字典类型,所以要换成字符串类型json.dumps(payload),但是之后的payload就是字符串类型了,所以直接写payload就行
                request = scrapy.Request(url=url_rl, body=payload, method="POST", callback=self.get_page_sum)
          #return [scrapy.Request(url=url_rl, body=json.dumps(payload), method="POST", callback=self.get_page_num)]
          yield request

  

这里的payload之前是字典类型,用return发送请求是需要字符串类型,所以要换成字符串类型json.dumps(payload),在用yield发送请求是需要的是字典类型,所以直接写payload就行,上面的是yield发送请求,所以body直接等于payload就可以了。
data_url = eval('{"orderby": "0","productTypeId": "%s","currentPage": "%s","pageSize": "12"}' %(id,i))
            request = scrapy.FormRequest(url=url, formdata=data_url, callback=self.get_parse,dont_filter=True)
            

  

posted @ 2019-05-02 17:08  乔儿  阅读(973)  评论(0编辑  收藏  举报