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
eternal403
V2EX  ›  Python

关于 github 上一个链家爬虫代码的困惑

  •  
  •   eternal403 · 2017-11-17 22:55:37 +08:00 · 6904 次点击
    这是一个创建于 2344 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想抓取链家的 app 数据,在 github 上找到一个看上去不错的项目: https://github.com/CaoZ/Fast-LianJia-Crawler,

    但是我对代码有两处有点疑惑,没看懂是怎么来的。

    1. 首先是 app/config.py 下面的这段代码
    self.lian_jia = {
                'ua': 'HomeLink7.7.6; Android 7.0',
                'app_id': '20161001_android',
                'app_secret': '7df91ff794c67caee14c3dacd5549b35'
            }
    

    作者是怎么知道 app_id 和 app_secret 的,我通过 Charles 抓包工具,看到 header 头信息有一大堆东西,没看到 app_id 和 app_secret 不知道作者是怎么试出来的。

    1. app/util/init.py

    get_token 这个方法,为什么作者知道 token 是这么产生的。

    def get_token(params):
        data = list(params.items())
        data.sort()
    
        token = config.lian_jia['app_secret']
    
        for entry in data:
            token += '{}={}'.format(*entry)
    
        token = hashlib.sha1(token.encode()).hexdigest()
        token = '{}:{}'.format(config.lian_jia['app_id'], token)
        token = base64.b64encode(token.encode()).decode()
    
        return token
    

    如果有哪位同学可以试试用 charles 抓取链家的包,查看 header 等相关信息。看看能不能找出相关规律。

    9 条回复    2019-08-03 22:21:15 +08:00
    Kirscheis
        1
    Kirscheis  
       2017-11-17 23:46:59 +08:00
    反编译 apk 可以得到一些信息,其它信息一般是抓包之后猜测算法得到的。。
    0987363
        2
    0987363  
       2017-11-18 09:54:48 +08:00 via iPhone
    反编译 app 出来的吧 以前弄过斗鱼的 然后斗鱼写进 so 了。。
    eternal403
        3
    eternal403  
    OP
       2017-11-18 15:05:13 +08:00
    @0987363 @Kirscheis 不知是不是我姿势不对,反编译了下链接的 apk,仍然没有找到代码,不知道是不是要处理混淆。不知道两位方不方便试试,谢谢。
    forestyuan
        4
    forestyuan  
       2017-11-18 20:24:37 +08:00
    兴许那段代码是 LJ 内部人士写的呢
    eternal403
        5
    eternal403  
    OP
       2017-11-19 10:39:15 +08:00
    @forestyuan 我也考虑过,哈哈
    caoz
        6
    caoz  
       2017-11-21 16:44:14 +08:00   ❤️ 3
    作者登场~

    我当时确实反编译了链家的安卓端 App,但是反编译了也没找到这些值到底在哪儿(毕竟不是专业 Java ...)😓 于是放狗一搜找到了一个项目: https://github.com/laopeng1990/realestate, 在 https://github.com/laopeng1990/realestate/blob/master/src/main/java/com/wpf/realestate/util/AuthUtils.java 找到了加密方式...

    不过后来知道了,这些值和加密部分是分别放到了 "libHomeLinkNdk.so" 和 "liblianjiajni.so" 中了,用 IDA Pro 打开处理下就可以看到了,比如 app_id 藏在这:

    RuoYun
        7
    RuoYun  
       2017-11-22 18:29:42 +08:00
    厉害了
    huhuanru
        8
    huhuanru  
       2019-08-03 07:59:49 +08:00
    @caoz 作者你好,这个程序已经不能用了,会报“无效的请求”,报错如下:
    Traceback (most recent call last):
    File "Fast-LianJia-Crawler/app/main.py", line 209, in <module>
    main()
    File "Fast-LianJia-Crawler/app/main.py", line 20, in main
    update_city(city_id)
    File "Fast-LianJia-Crawler/app/main.py", line 30, in update_city
    city_info = get_city_info(city_id)
    File "Fast-LianJia-Crawler/app/main.py", line 73, in get_city_info
    data = util.get_data(url, payload, method='POST')
    File "Fast-LianJia-Crawler\app\util\__init__.py", line 27, in get_data
    return parse_data(r)
    File "Fast-LianJia-Crawler\app\util\__init__.py", line 35, in parse_data
    raise Exception('请求出错了: ' + as_json['error'])
    Exception: 请求出错了: 无效的请求
    huhuanru
        9
    huhuanru  
       2019-08-03 22:21:15 +08:00
    @caoz 作者同学可以冒泡一下吗
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3889 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:16 · PVG 18:16 · LAX 03:16 · JFK 06:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.