2016-08-01 263 views
1

我想要做一些網站通過Javascript生成它的數據的網站。我已經通過,現在刮的這些方法是做了足夠的閱讀就在這裏要知道:用請求正確地構造一個XHR請求[python]

  1. 觀看螢火蟲網絡選項卡當您
  2. 隔離XHR請求,並重新請求會發生什麼他們在一個腳本。

所以,當我做1,POST請求被髮送到在這張截圖中可見的鏈接: enter image description here ,你也可以看到它得到響應。看起來不錯,對吧?

但是當我嘗試並重新創建請求&響應,有效載荷,我在Firebug後標籤下看到,在Python像這樣:

import requests 
from bs4 import BeautifulSoup 

payload = {"Max":999,"RectCoord":"89,-179,-89,179","Source":"","SortField":"NEWID()","OfficeName":"","FirstName" 
:"","LastName":"da","CityName":"","ZipCode":"","Category":"S","SecLanguageReq":"","OfficeCode":""} 

r = requests.post('http://search.cnyrealtor.com/MyAjaxService.asmx/MemberSearch', data=payload) 

print(r.content) 

我得到的顯示錯誤消息的頁面: Request format is unrecognized for URL unexpectedly ending in \'/MemberSearch\'

所以,我的問題是 - 爲什麼我在Firebug中的響應正常工作時得到該響應?我在Python腳本中的requests.post(url)行中錯過了什麼嗎?

回答

1

您需要將字典轉儲爲JSON並作爲有效負載發送。設置Content-Type請求標頭也很重要:

import json 
import requests 

payload = {"Max": 999, "RectCoord": "89,-179,-89,179", "Source": "", "SortField": "NEWID()", "OfficeName": "", 
      "FirstName": "", "LastName": "", "CityName": "", "ZipCode": "", "Category": "S", "SecLanguageReq": "", 
      "OfficeCode": ""} 

with requests.Session() as session: 
    session.get("http://search.cnyrealtor.com/SiteContent/SYR/MemberSearchSYR.aspx") 
    r = session.post('http://search.cnyrealtor.com/MyAjaxService.asmx/MemberSearch', data=json.dumps(payload), 
        headers={"Content-Type": "application/json; charset=UTF-8"}) 

    print(r.content) 
+0

非常感謝,@alecxe! – n1c9