2013-02-14 68 views
1

我試圖編寫腳本以編程方式登錄Google財經,查看我的投資組合,然後在我的桌面上顯示結果。我正在使用requests模塊,目前卡在「登錄」部分。CookieConflictError - 嘗試以編程方式登錄Google財經時的APISID

我不斷收到此錯誤requests.cookies.CookieConflictError: There are multiple cookies with name, 'APISID'

這裏是整個腳本,在line 48錯誤拋出。我猜這與requests keep-alive有關,連接無法正常回收?

#!/usr/bin/env python 

import getpass 
import re 
import requests 

email = raw_input("Enter your Google username: ") 
password = getpass.getpass("Enter your password: ") 

session = requests.Session() 

# Define URLs 
login_page_url = 'https://accounts.google.com/ServiceLogin?passive=true&service=finance' 
authenticate_url = 'https://accounts.google.com/ServiceLoginAuth?service=finance' 
gf_home_page_url = 'http://www.google.com/finance/portfolio' 

login_page_contents = session.get(login_page_url).text 

# Find GALX value 
galx_match_obj = re.search(r'name="GALX"\s*value="([^"]+)"', login_page_contents, re.IGNORECASE) 
galx_value = galx_match_obj.group(1) if galx_match_obj.group(1) is not None else '' 

# Find DSH value 
dsh_match_obj = re.search(r'id="dsh"\s*value="([^"]+)"', login_page_contents, re.IGNORECASE) 
dsh_value = dsh_match_obj.group(1) if dsh_match_obj.group(1) is not None else '' 

# Set up login credentials 
login_params = { 
    'Email': email, 
    'Passwd': password, 
    'continue': 'http://www.google.com/finance/portfolio', 
    'followup': 'http://www.google.com/finance/portfolio', 
    'service': 'finance', 
    'GALX': galx_value, 
    'pstMsg': 0, 
    'dnConn': '', 
    'checkConnection': '', 
    'timeStmp': '', 
    'secTok': '', 
    'bgresponse': 'js_disabled', 
    'PersistentCookie': 'yes' 
} 

print galx_value 
print dsh_value 

# Login 
r = session.post(authenticate_url, params=login_params) # <- Error thrown here 
print r.text 
exit 

回溯:

Traceback (most recent call last): 
    File "crawl.py", line 48, in <module> 
    r = session.post(authenticate_url, params=login_params) 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 358, in post 
    return self.request('POST', url, data=data, **kwargs) 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 312, in request 
    resp = self.send(prep, **send_kwargs) 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 426, in send 
    history = [resp for resp in gen] if allow_redirects else [] 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/sessions.py", line 163, in resolve_redirects 
    resp.cookies.update(cookiejar) 
    File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_abcoll.py", line 494, in update 
    self[key] = other[key] 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/cookies.py", line 246, in __getitem__ 
    return self._find_no_duplicates(name) 
    File "/Users/nathan/Development/Scripts/google-finance-crawler/requests/cookies.py", line 285, in _find_no_duplicates 
    raise CookieConflictError('There are multiple cookies with name, %r' % (name)) 
requests.cookies.CookieConflictError: There are multiple cookies with name, 'APISID' 
+0

你能不能給我們異常*全*回溯? – 2013-02-14 11:22:56

+0

是的,請參閱更新:)謝謝 – 2013-02-14 11:26:12

回答

3

這是在requests一個bug,請參閱issue 1189

目前提出的解決方法是簡單地刪除線的requests/sessions.py 163:

resp.cookies.update(cookiejar) 
+0

這是錯誤的行被刪除,但這是正確的問題。 (注意:我是「天才」人員,他們進行了修改並解決了後續的請求) – 2013-02-14 17:48:51

+0

@ sigmavirus24:我知道:-P我在這裏使用OP回溯行號,而不是來自您的修補程序的行號;我推測,自從您編寫補丁後,該文件中的其他一些內容已發生更改。:-) – 2013-02-14 17:51:05

+0

@ sigmavirus24:oops,不,我現在看到錯誤。糾正。 – 2013-02-14 17:55:00