webhose.io rest api的简单客户端库
webhose的Python项目详细描述
从python代码访问webhose.ioapi的简单方法
importwebhosewebhose.config(token=YOUR_API_KEY)forpostinwebhose.search("github"):print(post.title)
API键
要使用webhose.io api,您需要获得一个 每一个请求都使用。要获取api密钥,请在 https://webhose.io/auth/signup,然后进入 https://webhose.io/dashboard查看您的令牌。
安装
您可以从源安装:
$ git clone https://github.com/Buzzilla/webhose-python
$ cd webhose-python
$ python setup.py install
使用API
要开始,您需要导入库并设置访问令牌。 (用实际的api密钥替换你的api密钥)。
>>>importwebhose>>>webhose.config(token=YOUR_API_KEY)
现在您可以提出请求并检查结果:
>>>r=webhose.search("foobar")>>>r.total62>>>len(r.posts)62>>>r.posts[0].language'english'>>>r.posts[0].title'Putting quotes around dictionary keys in JS'
为了方便起见,response对象是iterable的,因此可以循环它 得到所有的结果。迭代器将创建附加的api请求 获取其他页。
>>>total_words=0>>>forpostinr:...total_words+=len(post.text.split(" "))...>>>print(total_words)56006
警告:如果您的搜索包含大量 结果。
完整文档
- 配置(令牌)
- 令牌-您的api密钥
- 搜索(query,token=none)
- 查询-作为搜索字符串或查询对象的搜索查询
- 令牌-如果需要,可以直接向搜索函数提供api密钥
查询对象
查询对象对应于出现在https://webhose.io/use上的高级搜索选项
查询对象具有以下成员:
- all_terms-字符串列表,所有字符串都必须出现在结果中
- some_terms-字符串列表,其中一些字符串必须出现在结果中
- phrase-结果中必须一字不差地出现的短语
- exclude-结果中不应出现的术语
- site_type-一个或多个discussions,news,blogs
- language-一个或多个语言名称,小写英文
- site-一个或多个站点名称,仅顶级(即yahoo.com而不是news.yahoo.com)
- title-标题中必须出现的术语
- body_text-必须出现在正文文本中的术语
查询对象实现__str__()方法,该方法显示结果搜索字符串
响应对象
响应对象具有以下成员:
- total-与此搜索匹配的文章总数
- more-此响应中未包含的帖子数
- posts-列出os post对象
- next-此搜索的下一个结果页的URL
- response-原始的requests响应
- get_next()-获取下一页结果的方法。返回新的响应对象
响应对象实现__iter__()方法,该方法可用于循环 所有与查询匹配的帖子。(自动取页)
后置和线程对象
post和thread对象包含从api返回的实际数据。 查阅https://webhose.io/documentation了解它们的结构。
轮询
如果要重复搜索,请在出现以下情况时执行操作 新结果,使用如下代码:
r=webhose.search("skyrim")whileTrue:forpostinr:perform_action(post)time.sleep(300)r=r.get_next()