发送XMLHttpRequest并获取Python3和请求的响应

时间:2016-07-25 00:23:56

标签: python python-3.x xmlhttprequest python-requests

我正在尝试从此页面https://www.docavenue.com/#!medecin:62210-avion/medecin-generaliste/degryse-gilles获得回复。

我在浏览器中打开DevTools,可以看到所需的请求。 Request info

我的代码:

import requests


url = 'https://www.docavenue.com/elastic/healthcaredirectory/_search'
data = {
    "from":0,
    "size":1,
    "min_score":0,
    "query":{
        "bool":{
            "must":[
                {
                    "constant_score":{
                        "query":{
                            "match_phrase_prefix":{
                                "fullName.simple":"degryse-gilles"
                            }
                        }
                    }
                },
                {
                    "constant_score":{
                        "query":{
                            "match_phrase":{
                                "specialties.simple":"medecin-generaliste"
                            }
                        }
                    }
                },
                {
                    "constant_score":{
                        "query":{
                            "prefix":{
                                "center.postalCode":"62"
                            }
                        }
                    }
                },
                {
                    "constant_score":{
                        "query":{
                            "match_phrase":{
                                "center.city.simple":"avion"
                            }
                        },
                        "boost":2
                    }
                }
            ],
            "should":[
                {
                    "constant_score":{
                        "query":{
                            "match":{
                                "hasFreeTimeslot":{
                                    "query": True
                                }
                            }
                        },
                        "boost":2
                    }
                },
                {
                    "constant_score":{
                        "query":{
                            "match":{
                                "clientdocAvenue":{
                                    "query": True
                                }
                            }
                        },
                        "boost":2
                    }
                },
                {
                    "constant_score":{
                        "query":{
                            "match":{
                                "clientLGC":{
                                    "query": True
                                }
                            }
                        },
                        "boost":1
                    }
                }
            ]
        }
    },
    "sort":[
        {
            "_score":{
                "order":"desc"
            }
        },
        {
            "lastName":{
                "order":"asc"
            }
        }
    ]
}

headers = {
            "Accept": "application/json",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4",
            "Connection": "keep-alive",
            "Content-Length": "728",
            "Content-Type": "application/json",
            "Cookie": "_gat=1; JSESSIONID=JPFr3DTsiE1rnNEmhNY5Ol9BocGhcFyVB9eemlpH.pdocaveweb02; BIGipServerpool-docavenue.cegedim-hds.fr-HTTP=!hO0vdO0hmRrQramJN+2PyIgcsB0pDo0KvdtLHbw+ziI+VMjPVBak3GeHdKZdGj3xQave1qLd0YeSikI=; _ga=GA1.2.1911756472.1469016537",
            "digest": "AKI20tURnFW6tp8nmtRNZVcxYEk/TViAUG5hOErsI4I=",
            "Host": "www.docavenue.com",
            "Origin": "https//www.docavenue.com",
            "Referer": "https//www.docavenue.com/",
            "timestamp": "2016-07-25T04:48:58.504+0500",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
            "X-HTTP-Method-Override": "POST"
            }

response = requests.post(url, data=data, headers=headers)
print(response)

我得到了回应[401]。 我的代码出了什么问题?

  1. 我是否将请求发送到请求信息中注明的网址?
  2. 标题是否合适?我应该添加另一个还是删除一些列出的内容?
  3. 我发送的数据是否正确?

1 个答案:

答案 0 :(得分:0)

您必须从cookie中写入会话数据。您可以在浏览器中使用DevTools查找这些数据。也许某些cookie数据丢失了。