2016-06-13 216 views
1

是否可以使用selenium webdriver向服務器發送多個HTTP POST請求?Python:在同一頁面上發送多個POST請求

例如如果用戶ID是自動遞增,前1000名追隨者跟着我,那會是可以運行像在哪裏POST請求不返回裝載的網站如下:

* *該網站顯然需要用戶登錄才能執行此類操作,因此採用硒方法。

driver = webdriver.Firefox() 
driver.get("http://example.com/myfollowers") 

for userid in range(1, 1001): 
    driver.post("http://example.com/unfollow/{}".format(userid)) 

driver.close() 

以下是對站點發布的發佈請求的代碼片段示例。

General: 
Request URL:http://example.com/unfollow/1 
Request Method:POST 
Status Code:200 OK 
Remote Address:192.168.1.1:80 
Response Headers 

Request headers: 
Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript 
Accept-Encoding:gzip, deflate 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:0 
Cookie:ob-i=1; 
Host:example.com 
Origin:http://example.com 
Referer:http://example.com/user/followings 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.26.02 Safari/537.36 
X-Requested-With:XMLHttpRequest 

回答

0

爲什麼它不工作?

「POST請求不返回重載」請解釋您的意思!

反正requests似乎更適合這個任務:

下面是一個簡單的例子:

import requests 

r = requests.get("http://example.com/myfollowers") 
print("myfollowers returned: " + r.content) 

for userid in range(1, 1001): 
    r = requests.post("http://example.com/unfollow/{}".format(userid)) 
    print("unfollow returned: " + r.content) 
+1

通過不返回一重裝我的意思是,它不提交表單並返回頁面刷新/新頁面(程序可以繼續在同一個選項卡中運行,而無需等待服務器響應)。 – AK47

+0

然後這仍然會工作。最後一行中的「r.content」將爲空。 –

0

不知道如何使用硒web驅動程序。但您可以使用AJAX發送多個發佈請求。它非常簡單的語言,你可以在2天內學習它,如果你知道一點點的JavaScript。你可以從w3schools學習它嘗試一下。