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

关于爬取知乎大 V 关注者的信息,遇到了点障碍,求大神前来指教!

  •  
  •   yamiedie · 2018-01-22 15:19:38 +08:00 · 2110 次点击
    这是一个创建于 2520 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如图,比如说我要爬大头死变态的关注人数 已知 URL 为:https://www.zhihu.com/people/wang-ni-ma-94/followers?page=1 通过抓包发现网站信息是动态加载的,然后顺藤摸瓜找到了对应的疑似 json 文件 api 接口为: https://www.zhihu.com/api/v4/members/wang-ni-ma-94/followers?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=20&limit=20

    在浏览器打开后是形如下的一段文本,疑似 json:

    // 20180122152224 // https://www.zhihu.com/api/v4/members/wang-ni-ma-94/followers?include=data%5B*%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=20&limit=20 { "paging": { "is_end": false, "totals": 614311, "previous": "http://www.zhihu.com/api/v4/members/wang-ni-ma-94/followers?include=data%5B%2A%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics&limit=20&offset=0", "is_start": false, "next": "http://www.zhihu.com/api/v4/members/wang-ni-ma-94/followers?include=data%5B%2A%5D.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics&limit=20&offset=40" }, "data": [ { "is_followed": false, "avatar_url_template": "https://pic2.zhimg.com/9256b7b963c816a5b5cae6b4b9b67bcf_{size}.jpg", "user_type": "people", "answer_count": 0, "is_following": false, "url": "http://www.zhihu.com/api/v4/people/44ced3e1aa77f2aac54447cc5219e523", "type": "people", "url_token": "zack-71-48", "id": "44ced3e1aa77f2aac54447cc5219e523", "articles_count": 0, "name": "zack", "headline": "", "gender": -1, "is_advertiser": false, "avatar_url": "https://pic2.zhimg.com/9256b7b963c816a5b5cae6b4b9b67bcf_is.jpg", "is_org": false, "follower_count": 0, "badge": [ ...... 之所以说疑似,是因为我在使用 requests.get(url).json()之后报错: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

    网上搜一了一下,说有可能不是合法的 json 文件 有哪位大兄 dei 知道怎么破吗? ps:因为这网站不能发图片,所以排版会丑一点,大家别见怪哈!

    yamiedie
        1
    yamiedie  
    OP
       2018-01-22 15:36:27 +08:00
    排版像坨 shi 啊,可是已经编辑不了了,大神们将就看着吧
    1130335361
        2
    1130335361  
       2018-01-22 15:54:42 +08:00   ❤️ 1
    直接 requests.get(url).json()是不行的,你打印 requests.get(url).content 看看返回的是什么

    需要加上 User-Agent 和 cookies 去请求
    1130335361
        3
    1130335361  
       2018-01-22 16:08:26 +08:00
    yamiedie
        4
    yamiedie  
    OP
       2018-01-22 16:09:37 +08:00
    @1130335361 请求前的准备我做完了,是没问题的,我检查了下,是因为刚开始的那个 // 20180122152224 的锅,因为这个的存在,这就不叫 json 文件了,只能是有一定规律的文件.现在问题是该怎么处理那个文件,好让它能让我反序列化.
    yamiedie
        5
    yamiedie  
    OP
       2018-01-22 16:10:09 +08:00
    @1130335361 连接挂了,好像....
    1130335361
        6
    1130335361  
       2018-01-22 16:15:43 +08:00 via iPhone
    @yamiedie 链接好的 要翻过去看
    yamiedie
        7
    yamiedie  
    OP
       2018-01-22 16:19:12 +08:00
    @1130335361 晚上才有梯子,总之,谢谢你了!
    yamiedie
        8
    yamiedie  
    OP
       2018-01-22 21:58:20 +08:00
    @1130335361 打印 response.content 是一串很长的字节流 b'....',但是我 decode 依然出错:invalid continuation byte...
    yamiedie
        9
    yamiedie  
    OP
       2018-01-23 00:38:22 +08:00
    @1130335361 问题解决了,看了你的 GitHub,豁然开朗!非常感谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1282 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:58 · PVG 07:58 · LAX 15:58 · JFK 18:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.