【问题标题】:Weird json value urllib python奇怪的json值urllib python
【发布时间】:2016-06-02 09:20:38
【问题描述】:

我正在尝试从该站点操作动态 JSON:

http://esaj.tjsc.jus.br/cposgtj/imagemCaptcha.do

它有 3 个元素,imagem,一个 base64,labelValorCaptcha,只是一条消息,和uuidCaptcha,一个通过参数传递的值以在下面的链接中播放声音:

http://esaj.tjsc.jus.br/cposgtj/somCaptcha.do?timestamp=1455996420264&uuidCaptcha=sajcaptcha_e7b072e1fce5493cbdc46c9e4738ab8a

当我通过浏览器进入第一个站点并在第二个链接中输入等号后的 uuidCaptha ("..uuidCaptcha=") 时,声音正常播放。我写了一个简单的代码来捕捉这些元素。

import urllib, json
url = "http://esaj.tjsc.jus.br/cposgtj/imagemCaptcha.do"
response = urllib.urlopen(url)
data = json.loads(response.read())
urlSound = "http://esaj.tjsc.jus.br/cposgtj/somCaptcha.do?timestamp=1455996420264&uuidCaptcha="
print urlSound + data['uuidCaptcha']

但我不知道发生了什么,uuidCaptcha 的捕获值不起作用。打开错误网页。

有人知道吗? 谢谢!

【问题讨论】:

  • 错误是什么?打印语句输出什么?
  • 另外你可能需要在开头插入当前时间戳:import time,然后将 urlSound 的定义更改为urlSound = "http://esaj.tjsc.jus.br/cposgtj/somCaptcha.do?timestamp=" + str(int(time.time())) + "&uuidCaptcha=" str(int(…)) 将时间戳四舍五入到最接近的秒,然后将其转换为字符串,以便在 URL 中使用
  • @CharlieHarding 打印输出应该是打开声音的链接,例如this
  • @CharlieHarding 时间戳只是为了防止缓存,不会阻止声音被打开。您可以在那里放置任何有效的时间戳。
  • 你说它应该输出这个字符串,但它没有。它输出什么:你能把错误贴在这里吗?可能你只是在运行 Python 3,在这种情况下 print 语句应该是一个函数:print(urlSound + data['uuidCaptcha'])

标签: python json urllib


【解决方案1】:

它对我有用。

$ cat a.py
#!/usr/bin/env python
# encoding: utf-8
import urllib, json


url = "http://esaj.tjsc.jus.br/cposgtj/imagemCaptcha.do"
response = urllib.urlopen(url)
data = json.loads(response.read())
urlSound = "http://esaj.tjsc.jus.br/cposgtj/somCaptcha.do?timestamp=1455996420264&uuidCaptcha="
print urlSound + data['uuidCaptcha']

$ python a.py
http://esaj.tjsc.jus.br/cposgtj/somCaptcha.do?timestamp=1455996420264&uuidCaptcha=sajcaptcha_efc8d4bc3bdb428eab8370c4e04ab42c

【讨论】:

  • 应该打开一个音频。
【解决方案2】:

正如我所说的@Charlie Harding,最好的方法是下载页面并获取 JSON 值,因为这个 JSON 是动态的,需要一个打开的 Web 链接才能存在。

更多信息here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多