鍍金池/ 問答/數據分析&挖掘  Python/ 模擬登陸csdn失敗,幫忙看下哪里有問題

            模擬登陸csdn失敗,幫忙看下哪里有問題

            從圖中得知表單需要哪些數據
            938215-20160809204500793-1563778880.png

            下面的兩行代碼保存cookies

            session = requests.Session()
            response = session.get(url, headers=headers).content

            下面的代碼獲取表單所需數據

            soup = BeautifulSoup(response, "lxml")
            # 解析頁面獲取表單必須的數據
            lt = soup.find(attrs={"type": "hidden", "name": "lt"})['value']
            execution = soup.find(attrs={"type": "hidden", "name": "execution"})['value']
            event_id = soup.find(attrs={"type": "hidden", "name": "_eventId"})['value']

            最后還是登錄失敗


            下面是完整代碼

            import requests
            from bs4 import BeautifulSoup
            
            url = 'https://passport.csdn.net/account/login'
            
            headers = {
                    "Host": "passport.csdn.net",
                    "Referer": "https://passport.csdn.net/account/login",
                    "User-Agent": 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89'
                                  ' Safari/537.36',
                    "Origin": "https://passport.csdn.net"
                }
            
            
            def login(username, password):
                captcha_solution = None
                captcha_id = None
                lt = None
                execution = None
                event_id = None
                post_data = {
                    "username": username,
                    "password": password,
                    "rememberMe": "true",
                }
                session = requests.Session()
                response = session.get(url, headers=headers).content
                soup = BeautifulSoup(response, "lxml")
                # 解析頁面獲取表單必須的數據
                lt = soup.find(attrs={"type": "hidden", "name": "lt"})['value']
                execution = soup.find(attrs={"type": "hidden", "name": "execution"})['value']
                event_id = soup.find(attrs={"type": "hidden", "name": "_eventId"})['value']
                # print(lt)
                # print(execution)
                # print(event_id)
                # 將獲取的數據加入表單
                post_data['lt'] = lt
                post_data['execution'] = execution
                post_data['_eventId'] = event_id
                #  所有參數準備完畢 開始登錄
                html = session.post(url, data=post_data, headers=headers)
                print(type(html.cookies))
                for key, value in html.cookies.items():
                    print(key+": "+value)
                print(html.text)
                # if html.url == 'https://www.csdn.net/':
                #     print(html.text)
                # else:
                #     print('fail')
            
            
            login('15200689458', '199704105896abc')
            回答
            編輯回答
            離觴

            clipboard.png
            可以通過捉包得出, 登錄url 不是https://passport.csdn.net/acc...
            而是這個https://passport.csdn.net/acc...
            session.post(url,data=post_data, headers=headers)
            中的url 改回 https://passport.csdn.net/acc... 就可以登錄

            2017年6月14日 11:43
            9久9久热精品视频在线观看