【发布时间】:2014-06-19 23:03:20
【问题描述】:
所以,我正在使用一个小的 python 脚本来尝试简单地打印出网站 'northwest.hall.' 的每一次出现,其中通配符 () 是一个非常大的数字从 url 中提取的 json 字符串。
到目前为止,我有这个: 导入urllib、json、re
url = 'http://graphite.website.com/render/?target=stats.web.northwest.hall.*&format=json'
response = urllib.urlopen(url)
data = json.loads(response.read())
code = re.findall('northwest', data)
print code
这应该返回正在解析的 json 字符串中的 30 个正则表达式的列表 north.hall.number,但我得到以下错误:
Traceback (most recent call last):
File "/Users/arin/Desktop/scripts/code_parser2.py", line 7, in <module>
code = re.findall('community', data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
Python 新手(你肯定知道)。 提前致谢。
【问题讨论】:
-
您是否验证了您所提取的数据与您在应用正则表达式之前所认为的一样?
-
当我尝试你的 URL 时,我得到了
404 - File or directory not found。 -
我也得到了
404,所以我使用httpbin.org/headers从服务器httpbin.org获取一些json