[問題] 爬蟲 網頁登入失敗

看板Python作者 ( )時間2年前 (2022/04/01 11:12), 編輯推噓1(101)
留言2則, 2人參與, 2年前最新討論串1/1
爬蟲網址: https://fe.xuanen.com.tw/fe02.aspx?module=login_page&files=login&PT=1 小弟嘗試獲取運動中心場地資訊 由於登入的驗證碼是由asp產生的 因此我的想法是先建一個session 再將asp產生的驗證碼圖片存下來後 再用人工辨識(之後再想辦法用自動辨識) 接著將帳號/密碼/驗證碼以post送出 若登入成功就能順利得到場地資訊 但嘗試以下程式碼仍無法順利登入成功 目前不知道該從哪方面debug起 還請版上高手賜教 程式碼: url_login = "https://fe.xuanen.com.tw/fe02.aspx?Module=login_page&files=login" url_book = "https://fe.xuanen.com.tw/fe02.aspx?module=net_booking&files=booking_place&StepFlag=2&PT=1&D=%s&D2=%s"%(date,section) url_cap = "https://fe.xuanen.com.tw/NewCaptcha.aspx" #build session session = requests.session() response=session.get(url_login) #get captcha valcode = session.get(url_cap) file.write(valcode.content) file.close() img = Image.open('cap.png') img.show() verify_code = input("Please enter verification code:") #send login info post_headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7', 'Connection': 'keep-alive', 'Content-Length': '347', 'Referer': 'https://fe.xuanen.com.tw/fe02.aspx?module=login_page&files=login&PT=1', 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary8iz7GC2K2CIsJHHu', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36' } form_data = { 'loginid': user_name, 'loginpw': passwd, 'Captcha_text': verify_code, } r = session.post(url_login, headers = post_headers, data=form_data) # get court info r = session.get(url_book) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.25.53.60 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1648782741.A.4EA.html

04/01 14:02, 2年前 , 1F
我猜是因為boundary是浮動的
04/01 14:02, 1F

04/01 17:05, 2年前 , 2F
感謝,將Content-Type從header中移除就可以了!
04/01 17:05, 2F
文章代碼(AID): #1YHcsLJg (Python)