gpt4 book ai didi

python - 如何在Python中异步使用twitter搜索API?

转载 作者:行者123 更新时间:2023-11-30 23:35:00 26 4
gpt4 key购买 nike

我读过这本书Introduction to Tornado 。它在使用twitter搜索API的应用程序中引入了tornado的异步特性。

code如下:

class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
query = self.get_argument('q')
client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(client.fetch,
"http://search.twitter.com/search.json?" + \
urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100}))
...
self.finish()

它使用 v1 twitter API 来搜索关键字。然而,新的v1.1 twitter API禁止使用非oauth请求。因此,我必须使用 oauth 库以及我的消费者 key 和访问 key 来请求 twitter 搜索 API。

def request_twitter(url, http_method = 'GET', post_body = '', http_headers = ''):
consumer = oauth.Consumer(key = consumer_key, secret = consumer_secret)
token = oauth.Token(key = access_token, secret = access_secret)
client = oauth.Client(consumer, token)
request = client.request(url, method = http_method, body = post_body, headers = http_headers)
return request

但是oauth客户端不提供异步请求的方式。所以我想知道如何使用 oauth 客户端向 python 中的 twitter API 发出异步请求?谢谢。

最佳答案

看看TwitterMixin随 Tornado 和研究一起提供its code一点。

关于python - 如何在Python中异步使用twitter搜索API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668895/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com