V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Aug 10, 2017 · 4847 views
    This topic created in 3193 days ago, the information mentioned may be changed or developed.

    比如这个页面: 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 replies    2017-08-11 16:41:53 +08:00
    zhihaofans
        1
    zhihaofans  
       Aug 10, 2017 via Android
    ua
    mrl1996
        2
    mrl1996  
       Aug 10, 2017
    要我说还是换个爬取
    visitantzj
        3
    visitantzj  
    OP
       Aug 10, 2017
    @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  
       Aug 10, 2017   ❤️ 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  
       Aug 10, 2017
    visitantzj
        6
    visitantzj  
    OP
       Aug 10, 2017
    @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
       Aug 10, 2017
    @q409195961 不好意思,这个是开始试另一个页面时候的 url 没改过来,我感觉应该是像楼上说的是 cookie 问题……自己先研究一下先
    q409195961
        8
    q409195961  
       Aug 10, 2017   ❤️ 1
    RorschachZZZ
        9
    RorschachZZZ  
       Aug 10, 2017
    不用爬吧,现在好多免费 api 接口
    visitantzj
        10
    visitantzj  
    OP
       Aug 10, 2017
    @q409195961

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

    @RorschachZZZ

    感觉天气网的数据比较正规点,其实原先也有 api 的,估计后来用的人多就关了
    yefuchao
        11
    yefuchao  
       Aug 10, 2017
    t123yh
        12
    t123yh  
       Aug 10, 2017 via Android
    用彩云天气的免费接口呀
    Bijiabo
        13
    Bijiabo  
       Aug 10, 2017
    楼主的需求是为了天气数据吧?可以找免费的 API 接口,感觉彩云天气的就不错
    jingniao
        14
    jingniao  
       Aug 10, 2017 via Android
    这叫我想起来大学一门课程结课作业做的一个简单的安卓天气了,当时做的很烂很烂……
    xd314697475
        15
    xd314697475  
       Aug 10, 2017
    需要天气接口的话,直接去
    http://openweathermap.org/current
    提供多种 api,免费使用
    wencan
        16
    wencan  
       Aug 11, 2017
    以前我用的中国天气网开放平台的接口 openweather.weather.com.cn 已经不可访问了
    现在中国天气网整了个智慧气象服务 不知道是不是升级版 http://smart.weather.com.cn/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 15:13 · PVG 23:13 · LAX 08:13 · JFK 11:13
    ♥ Do have faith in what you're doing.