# coding=utf-8
import requests
import json
import re
def itc_service_key():
url = "https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js"
req = requests.Session()
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0',
'Accept-Encoding' : 'identity'
}
response = req.get(url, headers=headers).content
pattern = re.compile(r"itcServiceKey = '(.*)'")
itc_service_key = pattern.findall(response)
print itc_service_key[0]
return itc_service_key[0]
def cookie():
get_cookie = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/wa/route?noext"
r = requests.Session()
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0',
}
response = r.get(get_cookie,headers=headers)
cookie = []
for keys in response.cookies:
cookie.append(keys.value)
return cookie[0]
itc_service_key = itc_service_key()
cookie = cookie()
print cookie
user = "xxxxxx"
password = "xxxxxxx"
req = requests.Session()
headers = {
'Content-Type' : 'application/json',
'X-Requested-With' : 'XMLHttpRequest',
'Accept' : 'application/json, text/javascript',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0',
'Content-Type' : 'application/x-www-form-urlencoded',
"Cookie" : cookie
}
data = {
"accountName": user,
"password": password,
"rememberMe": True
}
url = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey="+itc_service_key
print url
response = req.post(url, headers=headers, data=json.dumps(data))
print response.status_code
print response.content
1
cheetah 2016-10-15 11:41:20 +08:00
你这 requests.Session 的用法不对啊, Session 对象可以在不同请求之间自动保持 cookie ,你应该一直使用同一个 session 对象。
|
2
louzhumuyou OP @cheetah 好的 我试试,可能我对 session 的理解也不够透彻。
|
3
louzhumuyou OP @cheetah 依然报 500 错
|
4
odirus 2016-10-15 13:23:07 +08:00 via Android
我记得有个网站专用过去苹果 app 评价,评论哒
|
5
odirus 2016-10-15 13:30:37 +08:00
如果你的目的是爬 iTunes 上你们自己 APP 的评价或者评论,你可以爬这个网站( http://www.cqaso.com/),效率会更高一些
|
6
louzhumuyou OP @odirus 谢谢 我进去看过了 但是评论只有 9000 多条,比我们 itunes 里面少太多,我们里面有将近 30000 多条记录,所以这个还是延时了。我还是想登录 itunes 获取最直观。
|
7
cheetah 2016-10-15 19:42:33 +08:00
@louzhumuyou 新的代码是怎样的?
|
8
louzhumuyou OP @cheetah
``` # coding=utf-8 import requests import json import re url = "https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js" req = requests.Session() headers = { 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0', 'Accept-Encoding' : 'identity' } response = req.get(url, headers=headers).content pattern = re.compile(r"itcServiceKey = '(.*)'") itc_service_key = pattern.findall(response) print itc_service_key[0] get_cookie = "https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/wa/route?noext" headers = { 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0', } response = req.get(get_cookie,headers=headers) cookie = [] for keys in response.cookies: cookie.append(keys.value) print cookie user = "xxxx" password = "xxxx" headers = { 'Content-Type' : 'application/json', 'X-Requested-With' : 'XMLHttpRequest', 'Accept' : 'application/json, text/javascript', 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:46.0) Gecko/20100101 Firefox/46.0', 'Content-Type' : 'application/x-www-form-urlencoded', "Cookie" : cookie } data = { "accountName": user, "password": password, "rememberMe": True } url = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey="+itc_service_key[0] print url response = req.post(url, headers=headers, data=json.dumps(data)) print response.status_code # print response.headers # print response.data print response.content ``` |
9
louzhumuyou OP @cheetah 奇怪格式怎么没有,好吧,已经不重要了,修改成这样的了,还是 500
|
10
cheetah 2016-10-16 00:29:46 +08:00
@louzhumuyou 我没账号没法测试,不过你不用把 cookie 传来传去,用同一个 session 会自动处理的。另外 500 的响应内容是什么?
|
11
louzhumuyou OP @cheetah 500 的响应内容是空,你可以看一下` https://github.com/fastlane/fastlane/blob/master/spaceship/lib/spaceship/client.rb`我是看他处理了 cookie ,他用 ruby 写的,可以登录成功的。
|
12
louzhumuyou OP @cheetah 如果方便的话可以加我 qq 872489864 ,我再快速请教。
|