1

我正在尝试编写一个脚本以编程方式登录 Google Finance,查看我的投资组合,然后在我的桌面上显示结果。我正在使用该requests模块,目前停留在“登录”部分。

我不断收到此错误requests.cookies.CookieConflictError: There are multiple cookies with name, 'APISID'

这是整个脚本,错误抛出line 48。我猜它与请求保持活动有关并且连接没有正确回收?

#!/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'
4

1 回答 1

3

这是 中的一个错误requests,请参阅issue 1189

当前提议的修复是简单地删除第 163 行requests/sessions.py

resp.cookies.update(cookiejar)
于 2013-02-14T11:33:42.647 回答