V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
louzhumuyou
V2EX  ›  Python

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

  •  
  •   louzhumuyou · 2016-10-15 11:13:16 +08:00 · 3441 次点击
    这是一个创建于 2752 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求是如题所述。

    之前也用过 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
    
    12 条回复    2016-10-16 12:30:31 +08:00
    cheetah
        1
    cheetah  
       2016-10-15 11:41:20 +08:00
    你这 requests.Session 的用法不对啊, Session 对象可以在不同请求之间自动保持 cookie ,你应该一直使用同一个 session 对象。
    louzhumuyou
        2
    louzhumuyou  
    OP
       2016-10-15 11:43:19 +08:00
    @cheetah 好的 我试试,可能我对 session 的理解也不够透彻。
    louzhumuyou
        3
    louzhumuyou  
    OP
       2016-10-15 11:45:47 +08:00
    @cheetah 依然报 500 错
    odirus
        4
    odirus  
       2016-10-15 13:23:07 +08:00 via Android
    我记得有个网站专用过去苹果 app 评价,评论哒
    odirus
        5
    odirus  
       2016-10-15 13:30:37 +08:00
    如果你的目的是爬 iTunes 上你们自己 APP 的评价或者评论,你可以爬这个网站( http://www.cqaso.com/),效率会更高一些
    louzhumuyou
        6
    louzhumuyou  
    OP
       2016-10-15 14:04:16 +08:00
    @odirus 谢谢 我进去看过了 但是评论只有 9000 多条,比我们 itunes 里面少太多,我们里面有将近 30000 多条记录,所以这个还是延时了。我还是想登录 itunes 获取最直观。
    cheetah
        7
    cheetah  
       2016-10-15 19:42:33 +08:00
    @louzhumuyou 新的代码是怎样的?
    louzhumuyou
        8
    louzhumuyou  
    OP
       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
        9
    louzhumuyou  
    OP
       2016-10-15 22:22:31 +08:00
    @cheetah 奇怪格式怎么没有,好吧,已经不重要了,修改成这样的了,还是 500
    cheetah
        10
    cheetah  
       2016-10-16 00:29:46 +08:00
    @louzhumuyou 我没账号没法测试,不过你不用把 cookie 传来传去,用同一个 session 会自动处理的。另外 500 的响应内容是什么?
    louzhumuyou
        11
    louzhumuyou  
    OP
       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
        12
    louzhumuyou  
    OP
       2016-10-16 12:30:31 +08:00
    @cheetah 如果方便的话可以加我 qq 872489864 ,我再快速请教。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4801 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 04:00 · PVG 12:00 · LAX 21:00 · JFK 00:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.