我正在尝试从没有 API 的 Google 转销商面板中获取数据。
我可以使用 urllib2 和 cookiejar 成功登录,并且可以看到安全数据,但是我需要的数据是使用 AJAX 加载的。
当我尝试直接访问 AJAX 端点时,我得到一个 302 或 404 重定向。有任何想法吗?
这是我的代码:
import cookielib, urllib2, urllib, re, pprint
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'),
]
# Define URLs (borrowed this from Google voice script on Github)
login_page_url = 'https://accounts.google.com/ServiceLogin?service=grandcentral'
authenticate_url = 'https://accounts.google.com/ServiceLoginAuth?service=grandcentral'
gv_home_page_url = 'https://www.google.com/voice/#inbox'
# Load sign in page
login_page_contents = opener.open(login_page_url).read()
# 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 ''
# Set up login credentials
login_params = urllib.urlencode({
'Email' : "<my username>",
'Passwd' : "<my password>",
'continue' : 'https://www.google.com/voice/account/signin',
'GALX': galx_value
})
# Login
opener.open(authenticate_url, login_params)
# Open GV home page
gv_home_page_contents = opener.open(gv_home_page_url).read()
# Fine _rnr_se value
key = re.search('name="_rnr_se".*?value="(.*?)"', gv_home_page_contents)
if not key:
logged_in = False
else:
logged_in = True
print "Logged in!"
reseller_home = opener.open("https://www.google.com/a/cpanel/<my reseller domain>/Reseller2Customers#Reseller2Customers")
print reseller_home.read() #prints data behind the login, http status code 200
opener.addheaders += [
('Referer', 'https://www.google.com/a/cpanel/<my reseller domain>/Reseller2Customers'),
]
reseller_ajax = opener.open("https://www.google.com/a/cpanel/<my reseller domain>/Reseller/GetSubscriptions?sortField=CREATION_DATE&sortAscending=true&nextToken=&customerDomainPrefix=covision.com").read()
#this gives me error 302 or 404