无法从发布请求中获得回复

时间:2017-05-24 22:40:30

标签: python scrape

我正在尝试向https://www.apartments.com发出POST请求,但我无法得到任何回复。这是我到目前为止所写的内容:

import requests
payload = {'t': '13555 SW Jenkins Rd, Beaverton, OR 97005', 'l': [-79.55213, 40.3322]}
r = requests.post("https://www.apartments.com/services/geography/search/", data=payload)

print r.status_code
print r.text  # return null

1 个答案:

答案 0 :(得分:0)

如果您使用simplejson,则可以将有效负载转换为JSON。当然,你仍然需要担心标题中的内容类型,所以我也补充说:

>>> import requests
>>> from simplejson import dumps as D
>>> payload = {'t': '13555 SW Jenkins Rd, Beaverton, OR 97005', 'l': [-79.55213, 40.3322]}
>>> headers = {'content-type': 'application/json'}
>>> r = requests.post("https://www.apartments.com/services/geography/search/",
...                    data=D(payload),
...                    headers=headers)
>>> 
>>> print r.status_code
200
>>> print r.text
[{"ID":"mp0jdmj","Display":"13555 SW Jenkins Rd, Beaverton, OR 97005","GeographyType":8,"Address":{"City":"Beaverton","State":"OR","StreetName":"13555 SW Jenkins Rd"},"Location":{"Latitude":45.50167,"Longitude":-122.81654}}]
>>>