求助关于用 python requests 登录 itunes connect 获取关于我们自己 app 的评分评价

2016-10-15 11:13:16 +08:00
 louzhumuyou

需求是如题所述。

之前也用过 requests 爬过拉勾网的信息,准备用 requests 登录 itunes 的时候发现各种问题,现在在参考 fastlane spaceship 中有关用 ruby 登录 itunes 的脚本,可是对 ruby 不是很懂,各种看不透,以下附上, fastlane spaceship

关于登录 itunes 的脚本位置: https://github.com/fastlane/fastlane/blob/master/spaceship/lib/spaceship/client.rb 另外附上已经写好的 python 部分。 问题是: post 之后, code 为 500 ,肯定是不对的。感觉在获取 cookie 部分我做的和 spaceship 不一致,但是又不知道应该怎么修改,真心求助。写的很简单,只是想能先登录。

# 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
3447 次点击
所在节点    Python
12 条回复
cheetah
2016-10-15 11:41:20 +08:00
你这 requests.Session 的用法不对啊, Session 对象可以在不同请求之间自动保持 cookie ,你应该一直使用同一个 session 对象。
louzhumuyou
2016-10-15 11:43:19 +08:00
@cheetah 好的 我试试,可能我对 session 的理解也不够透彻。
louzhumuyou
2016-10-15 11:45:47 +08:00
@cheetah 依然报 500 错
odirus
2016-10-15 13:23:07 +08:00
我记得有个网站专用过去苹果 app 评价,评论哒
odirus
2016-10-15 13:30:37 +08:00
如果你的目的是爬 iTunes 上你们自己 APP 的评价或者评论,你可以爬这个网站( http://www.cqaso.com/),效率会更高一些
louzhumuyou
2016-10-15 14:04:16 +08:00
@odirus 谢谢 我进去看过了 但是评论只有 9000 多条,比我们 itunes 里面少太多,我们里面有将近 30000 多条记录,所以这个还是延时了。我还是想登录 itunes 获取最直观。
cheetah
2016-10-15 19:42:33 +08:00
@louzhumuyou 新的代码是怎样的?
louzhumuyou
2016-10-15 22:21:26 +08:00
@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


```
louzhumuyou
2016-10-15 22:22:31 +08:00
@cheetah 奇怪格式怎么没有,好吧,已经不重要了,修改成这样的了,还是 500
cheetah
2016-10-16 00:29:46 +08:00
@louzhumuyou 我没账号没法测试,不过你不用把 cookie 传来传去,用同一个 session 会自动处理的。另外 500 的响应内容是什么?
louzhumuyou
2016-10-16 05:38:09 +08:00
@cheetah 500 的响应内容是空,你可以看一下` https://github.com/fastlane/fastlane/blob/master/spaceship/lib/spaceship/client.rb`我是看他处理了 cookie ,他用 ruby 写的,可以登录成功的。
louzhumuyou
2016-10-16 12:30:31 +08:00
@cheetah 如果方便的话可以加我 qq 872489864 ,我再快速请教。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/312963

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX