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

请教爬虫问题,浏览器能访问到的页面,爬虫时某些 id 能访问到,有些不能。

  •  
  •   JCZ2MkKb5S8ZX9pq · 2018-11-20 18:02:48 +08:00 · 1144 次点击
    这是一个创建于 1981 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 为了做课题,通过 uid 读取微博的用户基本信息。
    • 从 chrome 模拟 h5 访问。
    • 某些用户如 uid1 可以正常访问。
    • 某些用户如 uid2 则不行,chrome 中可正常访问。
    • 排除网络不稳定因素。

    问题

    • uid2 访问失败的原因是什么?
    • 有没有方法区分 {访问失败} 和 {用户被封禁销号}。
    import requests
    
    
    def get_user_info(uid):
        url = 'https://m.weibo.cn/api/container/getIndex'
        headers = {
            'Accept': 'application/json, text/plain, */*',
            'DNT': '1',
            'MWeibo-Pwa': '1',
            'Referer': 'https://m.weibo.cn/u/1707254184',
            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
            'X-Requested-With': 'XMLHttpRequest',
        }
    
        headers['Referer'] = f'https://m.weibo.cn/u/{uid}'
        p = {'type': 'uid',
             'value': uid,
             'containerid': '100505%s' % uid}
        body = request(url, headers=headers, params=p)
        print(body)
    
    
    uid1 = 1692016845
    uid2 = 1996669711
    get_user_info(uid1)
    get_user_info(uid2)
    
    JCZ2MkKb5S8ZX9pq
        1
    JCZ2MkKb5S8ZX9pq  
    OP
       2018-11-20 18:27:16 +08:00
    fiddler 下用安卓模拟器测试了下,浏览器访问 uid2,提示证书有问题(已安装 fiddler 证书),会不会是微博方的 https 的问题?
    但我不是很懂,有人能费心解释一下嘛?
    freshmanc
        2
    freshmanc  
       2018-11-20 19:46:34 +08:00
    >>>get_user_info(uid2)
    {'ok': 0, 'msg': '这里还没有内容', 'data': {'cards': []}}

    does uid2 really exist?
    1130335361
        3
    1130335361  
       2018-11-21 10:07:12 +08:00   ❤️ 1
    经过测试,查看这个博主好像要登录之后才能看,不知道是什么机制
    楼主可以用隐身模式打开这两个链接试试
    http://weibo.com/u/1996669711
    https://m.weibo.cn/u/1996669711
    JCZ2MkKb5S8ZX9pq
        4
    JCZ2MkKb5S8ZX9pq  
    OP
       2018-11-21 17:20:32 +08:00
    @1130335361 后来访问失败的都加 cookie 重取了。不过不知道微博网页版的这个 cookie 管用多久,客户端 api 倒是能用很久,不过那个访问多了又有 403 限制,之后再看吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   977 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:21 · PVG 04:21 · LAX 13:21 · JFK 16:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.