[英]gathering tweets from twitter api python
i am trying to run this script to gather tweets from twitter but it wont run, it keeps saying syntax invalid. 我正在尝试运行此脚本以收集来自Twitter的推文,但不会运行,它一直说语法无效。 I want to gather the tweets adn save them to an amazon instance,but currently it wont run, i have all the twitter keys and access tokens created and inserted into the script bellow also
我想收集推文并将其保存到亚马逊实例,但目前无法运行,我创建了所有推特键和访问令牌,并将它们插入脚本中
Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. 在Win32上使用Python 3.3.4(v3.3.4:7ff62415e426,2014年2月10日,18:13:51)[MSC v.1600 64位(AMD64)]输入“ copyright”,“ credits”或“ license()”以获取更多信息信息。
>>> import oauth2 as oauth
import urllib2 as urllib
access_token_key = "****"
access_token_secret = "******"
consumer_key = "******"
consumer_secret = "******"
_debug = 0
oauth_token = oauth.Token(key=access_token_key, secret=access_token_secret)
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
http_method = "GET"
http_handler = urllib.HTTPHandler(debuglevel=_debug)
https_handler = urllib.HTTPSHandler(debuglevel=_debug)
'''
Construct, sign, and open a twitter request
using the hard-coded credentials above.
'''
def twitterreq(url, method, parameters):
req = oauth.Request.from_consumer_and_token(oauth_consumer,
token=oauth_token,
http_method=http_method,
http_url=url,
parameters=parameters)
req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)
headers = req.to_header()
if http_method == "POST":
encoded_post_data = req.to_postdata()
else:
encoded_post_data = None
url = req.to_url()
opener = urllib.OpenerDirector()
opener.add_handler(http_handler)
opener.add_handler(https_handler)
response = opener.open(url, encoded_post_data)
return response
def fetchsamples():
url = "https://stream.twitter.com/1/statuses/sample.json"
parameters = []
response = twitterreq(url, "GET", parameters)
for line in response:
print line.strip()
if __name__ == '__main__':
fetchsamples()
The very first line of you code contains syntactic error due to these three greater-than signs: 由于这三个大于号,您的代码的第一行包含语法错误:
$ cat z.py
>>> import oauth2 as oauth
$ python z.py
File "z.py", line 1
>>> import oauth2 as oauth
^
SyntaxError: invalid syntax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.