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

请教,怎么爬天气网的 json 数据

  •  
  •   visitantzj · 2017-08-10 18:00:26 +08:00 · 3962 次点击
    这是一个创建于 2422 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如这个页面: http://m.weather.com.cn/maqi/101020100.shtml

    json 格式数据类似: http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/101020100.html

    整个 Request Headers(Referer, cookie 之类)完全设了还是返回 403,请教一下问题出在哪里

    16 条回复    2017-08-11 16:41:53 +08:00
    zhihaofans
        1
    zhihaofans  
       2017-08-10 18:02:00 +08:00 via Android
    ua
    mrl1996
        2
    mrl1996  
       2017-08-10 18:02:55 +08:00
    要我说还是换个爬取
    visitantzj
        3
    visitantzj  
    OP
       2017-08-10 18:05:43 +08:00
    @zhihaofans
    也设了,chrome 里正常访问的 request header 我都给弄了进去,还是不行

    {'Host': 'd1.weather.com.cn',
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    'Accept': '*/*'',
    'Referer': 'http://m.weather.com.cn/mweather/101020100.shtml',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Cookie': 'BIGipServerd1src_pool=1874396221.20480.0000'}
    lxml
        4
    lxml  
       2017-08-10 18:13:39 +08:00   ❤️ 1
    ```
    import requests

    cookies = {
    # 自己填写
    }

    headers = {
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
    'Accept': '*/*',
    'Referer': 'http://m.weather.com.cn/maqi/101020100.shtml',
    'Connection': 'keep-alive',
    }

    params = (
    ('_', '14483821791309'),
    )

    a = requests.get('http://d1.weather.com.cn/aqi_all/101020100.html', headers=headers, params=params, cookies=cookies)
    a.encoding = 'utf-8'
    print(a.text)
    ```
    刚刚用 curl 生成了一份代码,实测是可以的,你可能是没带 cookie ?
    q409195961
        5
    q409195961  
       2017-08-10 18:16:14 +08:00
    visitantzj
        6
    visitantzj  
    OP
       2017-08-10 18:24:56 +08:00
    @lxml 感谢老兄!

    看来应该是 cookie 的问题,我这写法里 urllib.request.Request 应该是没把 cookie 传递出去。非常感谢!知道大方向自己就好去 google 了

    url = r"http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/101020100.html"
    headers = {
    'Host': 'd1.weather.com.cn',
    'Connection': 'keep-alive',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    'Accept': '*/*',
    'Referer': 'http://m.weather.com.cn/maqi/101020100.shtml',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Cookie': 'f_city=%E9%95%87%E6%B1%9F%7C101190301%7C; BIGipServerd1src_pool=1874396221.20480.0000'
    }
    req = urllib.request.Request(url= url, headers=headers)

    resp = urllib.request.urlopen(url)
    respBytes = resp.read()
    html = respBytes.decode('utf-8')
    visitantzj
        7
    visitantzj  
    OP
       2017-08-10 18:26:38 +08:00
    @q409195961 不好意思,这个是开始试另一个页面时候的 url 没改过来,我感觉应该是像楼上说的是 cookie 问题……自己先研究一下先
    q409195961
        8
    q409195961  
       2017-08-10 18:33:22 +08:00   ❤️ 1
    RorschachZZZ
        9
    RorschachZZZ  
       2017-08-10 18:40:06 +08:00
    不用爬吧,现在好多免费 api 接口
    visitantzj
        10
    visitantzj  
    OP
       2017-08-10 18:48:21 +08:00
    @q409195961

    正无地自容中……其实问题很无厘头 resp = urllib.request.urlopen(**url**)里 url 忘记改成 req 了

    @RorschachZZZ

    感觉天气网的数据比较正规点,其实原先也有 api 的,估计后来用的人多就关了
    yefuchao
        11
    yefuchao  
       2017-08-10 19:20:08 +08:00
    t123yh
        12
    t123yh  
       2017-08-10 20:02:45 +08:00 via Android
    用彩云天气的免费接口呀
    Bijiabo
        13
    Bijiabo  
       2017-08-10 20:04:22 +08:00
    楼主的需求是为了天气数据吧?可以找免费的 API 接口,感觉彩云天气的就不错
    jingniao
        14
    jingniao  
       2017-08-10 20:39:23 +08:00 via Android
    这叫我想起来大学一门课程结课作业做的一个简单的安卓天气了,当时做的很烂很烂……
    xd314697475
        15
    xd314697475  
       2017-08-10 20:58:31 +08:00
    需要天气接口的话,直接去
    http://openweathermap.org/current
    提供多种 api,免费使用
    wencan
        16
    wencan  
       2017-08-11 16:41:53 +08:00
    以前我用的中国天气网开放平台的接口 openweather.weather.com.cn 已经不可访问了
    现在中国天气网整了个智慧气象服务 不知道是不是升级版 http://smart.weather.com.cn/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1197 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:16 · PVG 07:16 · LAX 16:16 · JFK 19:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.