我正在編寫一個腳本,該腳本將 url、用戶名和密碼作為參數(shù),發(fā)出 Web 請求并記錄結(jié)果。我想使用一個包含許多站點/憑據(jù)的 csv 文件,并循環(huán)每個條目的請求,如果有任何異常/錯誤,我只想跳到文件中的下一個條目,但我的腳本在第一次運行后掛起循環(huán)目前我只是通過命令行標志手動傳遞參數(shù),但這不可行,因為手動執(zhí)行需要很長時間當我嘗試創(chuàng)建循環(huán)并從文件中提取參數(shù)時,我的函數(shù)運行一次然后掛起無限期地,無論是否有例外,都會發(fā)生這種情況。#currently Im using command line arguments to manually input the #arguments for the function below, which works fine but I want to pull #arguments from a list in a csv file (see comments at end for what ive #tried)def webrequest(username,password): url = site headers = {'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36", 'Accept-Encoding' : 'none' } payload = {'log': username, 'pwd': password } session = requests.session r = session.post(url, headers=headers, data=payload) if r.status_code == 404 print "404" #end function, move on to next line in csv if r.status_code == 200 print r.text # move on to next line in csv#file = open(file, r)#with open(file) as f:# for line in f:# args = line.split(",")# webrequest(args[0],args[1],args[2])我希望當函數(shù)結(jié)束時使用 csv 文件中的下一行重新開始,但它沒有......它只是掛在命令行上
1 回答

qq_笑_17
TA貢獻1818條經(jīng)驗 獲得超7個贊
嘗試使用 timeout 參數(shù)的 session.post() 。詳情請見:http : //docs.python-requests.org/en/master/user/advanced/#timeouts
很可能您的 Web 服務(wù)器沒有返回響應(yīng),因此您覺得您的功能被卡住了。
添加回答
舉報
0/150
提交
取消