2017-04-18 70 views
0

是python中的新手。提取下面的代碼從在線帖子登錄到網站,但出現錯誤。如何修復這個執行登錄網站的python代碼

請幫忙解決這個問題,並說明將幫助我

import requests 

with requests.Session() as c: 
    EMAIL = '[email protected]' 
    PASSWORD = '[email protected]' 
    URL = 'https://www.linkedin.com/' 
    c.get(URL) 
    token = c.cookies['CsrfParam'] 
    # This is the form data that the page sends when logging in 
    login_data = {loginCsrfParam:token, session_key:EMAIL, session_password:PASSWORD} 
    # Authenticate 
    r = c.post(URL, data=login_data) 
    # Try accessing a page that requires you to be logged in 
    r = c.get('https://www.linkedin.com/feed/') 
    print r.content 

我堅持下面的錯誤:

C:\Python27>python website.py 
Traceback (most recent call last): 
    File "website.py", line 8, in <module> 
    token = c.cookies['CsrfParam'] 
    File "C:\Python27\lib\site-packages\requests\cookies.py", line 329, in __getitem__ 
    return self._find_no_duplicates(name) 
    File "C:\Python27\lib\site-packages\requests\cookies.py", line 400, in _find_no_duplicates 
    raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path)) 
KeyError: "name='CsrfParam', domain=None, path=None" 

回答

0

你得到錯誤的原因是,你調用一個來自列表中的值爲空。要調用列表中的第一個項目,可以使用list [0]。在這種情況下,你所調用的列表是空的,所以第一個值不存在,因此錯誤。

我已經運行了你的代碼,沒有'recaptcha-token'的@id值,這就是代碼返回一個空列表的原因。唯一需要使用recaptcha標記的地方是註冊,所以我建議嘗試登錄而不創建authenticity_token。

+0

嗨!我跑出它令牌。它沒有顯示任何錯誤,但它並沒有打開網頁。我希望此代碼打開網頁並使用提供的憑據登錄。 –

+0

我已經嘗試了很多與請求的變化,我不能得到它的工作。根據我的經驗,Selenium傾向於使用反編程系統,因此可能需要查看此文章:https://www.quora.com/How-do-I-log-into-Quora-using-Python-since-they -dont-have-an-API。另外還有一個Quora模塊:https://pypi.python.org/pypi/quora/但是這不會登錄,所以它可能沒有你需要的功能。對不起,我忍不住了。 – TLOwater