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

requests 获取数据咨询

  •  
  •   coolloves · 2017-06-22 09:21:29 +08:00 · 2478 次点击
    这是一个创建于 2493 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想获取中国天气网的天气预警列表,http://www.weather.com.cn/alarm/warninglist.shtml
    比如获取北京的暴雨黄色预警,看了下,是通过 js 请求 http://product.weather.com.cn/alarm/grepalarm.php?areaid=10101[\d]{0,2}&type=02&level=02&_=1498094080205
    可是为什么我直接打开这个地址是 404 呢,用 requests 获取的和浏览器打开一样,请问这个请求应该如何处理呢,谢谢

    import requests
    import time

    url = 'http://product.weather.com.cn/alarm/grepalarm.php?areaid=10101[\d]{0,2}&type=02&level=02&_=' + str(int(time.time() * 1000))
    res = requests.get(url)
    print res.content
    12 条回复    2017-06-22 10:18:46 +08:00
    5dkgansm
        1
    5dkgansm  
       2017-06-22 09:24:42 +08:00
    你仔细想想你的请求头和浏览器一样了吗?
    coolloves
        2
    coolloves  
    OP
       2017-06-22 09:26:23 +08:00
    @5dkgansm 我加了 header 也是一样
    用浏览器直接打开这个 url 也是 404
    5dkgansm
        3
    5dkgansm  
       2017-06-22 09:30:51 +08:00
    http://www.weather.com.cn/alarm/newalarmcontent.shtml?file=1012407-20170622082417-0902.html 这个是随便点开的地址,你访问的地址是什么?难道是生成的不是抓的? 代码贴全看看。
    Troevil
        4
    Troevil  
       2017-06-22 09:34:36 +08:00
    带上 Cookie 即可
    Troevil
        5
    Troevil  
       2017-06-22 09:35:32 +08:00
    Troevil
        6
    Troevil  
       2017-06-22 09:36:53 +08:00
    coolloves
        7
    coolloves  
    OP
       2017-06-22 09:43:12 +08:00
    @5dkgansm 感谢耐心回复哈
    我的目的就是想获取下预警列表,然后如果有我当地的预警,比如北京的,然后我在访问具体的预警内容,学 python 不久,谢谢.

    import requests
    import time

    url = 'http://product.weather.com.cn/alarm/grepalarm.php?areaid=[0-9]{5,7}&_=' + str(int(time.time() * 1000))
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
    'Refefer':'http://www.weather.com.cn/alarm/warninglist.shtml',
    'DNT': 1,
    'Host':'product.weather.com.cn'}
    r = requests.Session()
    pre_res = r.get('http://www.weather.com.cn/alarm/warninglist.shtml',headers=headers)
    res = r.get(url,headers=headers)
    print res.content
    coolloves
        8
    coolloves  
    OP
       2017-06-22 09:47:03 +08:00
    @Troevil 拼写错误了...多谢
    @5dkgansm 谢谢指点,是 referer 拼写错了,
    PythoneerDev6
        9
    PythoneerDev6  
       2017-06-22 09:50:06 +08:00
    是的 得先分析头信息。 我的公众号 DeveloperPython 欢迎你。
    liukangxu
        10
    liukangxu  
       2017-06-22 10:11:49 +08:00 via Android
    @Troevil 这是个什么工具?求教
    xiaomeimei
        11
    xiaomeimei  
       2017-06-22 10:14:07 +08:00   ❤️ 1
    @liukangxu chrome 插件,postman
    Troevil
        12
    Troevil  
       2017-06-22 10:18:46 +08:00   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2847 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 11:25 · PVG 19:25 · LAX 04:25 · JFK 07:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.