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

求一个获取汇率的 Python 的脚本 之前的全不能用了。。。

  •  
  •   woshichuanqilz · 2017-11-04 00:17:10 +08:00 · 3333 次点击
    这是一个创建于 2358 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个是我之前找的我一个脚本 现在不行了

    #python
    import json,urllib
    from urllib.parse import urlencode
    import urllib.request
    
    url = 'http://api.k780.com'
    params = {
      'app' : 'finance.rate',
      'scur' : 'USD',
      'tcur' : 'CNY',
      'appkey' : 'APPKEY',
      'sign' : 'SIGN',
      'format' : 'json',
    }
    params = urlencode(params)
    
    f = urllib.request.urlopen('%s?%s' % (url, params))
    nowapi_call = f.read()
    #print content
    a_result = json.loads(nowapi_call)
    if a_result:
      if a_result['success'] != '0':
        print(a_result['result'])
      else:
        print(a_result['msgid']+' '+a_result['msg'])
    else:
      print('Request nowapi fail.')
    

    试了几个类似的程序也都不行了 结果都是

    (10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)))

    所以想问各位有没有什么好办法。

    9 条回复    2017-11-14 11:45:16 +08:00
    CEBBCAT
        1
    CEBBCAT  
       2017-11-04 00:31:39 +08:00 via Android
    应该是接口变了

    https://www.nowapi.com/api/finance.rate_cnyquot

    听说雅虎也提供 API,自己改改代码吧
    noqwerty
        2
    noqwerty  
       2017-11-04 04:38:07 +08:00
    雅虎 Google 都有 API,或者图省事的话 https://pypi.python.org/pypi/forex-python 直接装包呗。
    chen2016
        3
    chen2016  
       2017-11-04 07:51:47 +08:00 via Android
    接口不是固定不变的。这种情况要么自己写,要么找人付费改
    jianghu52
        4
    jianghu52  
       2017-11-04 11:32:47 +08:00
    依照楼主的代码,我这边有一点小问题。就是在第七行。url = 'http://api.k780.com' 要改成 url = 'http://api.k780.com/'
    这样我就能得到正确的 json。当然,我用的 appkey,sign 都是他事例中提供的值,我没有注册,所以我觉得可能不是脚本不对,而是楼主需要重新取一下 appkey 了吧?
    woshichuanqilz
        5
    woshichuanqilz  
    OP
       2017-11-04 14:12:51 +08:00
    @noqwerty

    这个试过了 还是不行的

    ![pic]( http://7xpvdr.com1.z0.glb.clouddn.com/1104141221.png)
    woshichuanqilz
        6
    woshichuanqilz  
    OP
       2017-11-04 14:16:25 +08:00
    dd99iii
        7
    dd99iii  
       2017-11-04 15:57:10 +08:00
    jianghu52
        8
    jianghu52  
       2017-11-04 19:27:34 +08:00
    @woshichuanqilz 一模一样的代码,我这里移动宽带。运行显示成功。这个问题在我这里再现不了,抱歉,帮不了你了。
    ws1689371
        9
    ws1689371  
       2017-11-14 11:45:16 +08:00
    这么多 api,重新找个 ok 的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2833 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:02 · PVG 22:02 · LAX 07:02 · JFK 10:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.