Python 如何使用urllib库通过CURL从Twiter搜索API检索json文件
我刚刚学会了如何通过应用程序API访问数据,我遇到了一个麻烦。 我目前正在尝试从Twitter搜索API检索json文件。API的文档说明使用CURL是为了通过HTTP GET访问数据。以下是curl格式:Python 如何使用urllib库通过CURL从Twiter搜索API检索json文件,python,api,curl,twitter,urllib,Python,Api,Curl,Twitter,Urllib,我刚刚学会了如何通过应用程序API访问数据,我遇到了一个麻烦。 我目前正在尝试从Twitter搜索API检索json文件。API的文档说明使用CURL是为了通过HTTP GET访问数据。以下是curl格式: curl "https://api.twitter.com/1.1/tweets/search/:product/:label.json?query=TwitterDev%20%5C%22search%20api%5C%22&maxResults=500&fromDate=
curl "https://api.twitter.com/1.1/tweets/search/:product/:label.json?query=TwitterDev%20%5C%22search%20api%5C%22&maxResults=500&fromDate=<yyyymmddhhmm>&toDate=<yyyymmddhhmm>" -H "Authorization: Bearer TOKEN"
当我尝试访问上面的用户_timeline tweets检索器时,它会工作
TWITTER_URL = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
但它与搜索API不兼容,我想这是因为CURL。
我已经检查了我使用的参数是否是文档要求使用的必要参数
此外,twurl库用于访问令牌并同时处理url和参数以生成最终url。(据我所知)
下面是twurl代码:
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
HTTPError: Unprocessable Entity
import urllib.request, urllib.parse, urllib.error
import oauth
import hidden
# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py
def augment(url, parameters):
secrets = hidden.oauth()
consumer = oauth.OAuthConsumer(secrets['consumer_key'], secrets['consumer_secret'])
token = oauth.OAuthToken(secrets['token_key'], secrets['token_secret'])
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
token, http_method='GET', http_url=url,
parameters=parameters)
#below is the main function galls!!
oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
consumer, token)
return oauth_request.to_url()
def oauth():
return {"consumer_key": "2HZq407wF.................",
"consumer_secret": "OsemLubDmCcQq5Y3q............",
"token_key": "75230340-2SGPJWWn..............",
"token_secret": "NZcII332Y3EI.............."}
下面是隐藏的,其中存储了所有密钥和令牌:
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
HTTPError: Unprocessable Entity
import urllib.request, urllib.parse, urllib.error
import oauth
import hidden
# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py
def augment(url, parameters):
secrets = hidden.oauth()
consumer = oauth.OAuthConsumer(secrets['consumer_key'], secrets['consumer_secret'])
token = oauth.OAuthToken(secrets['token_key'], secrets['token_secret'])
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,
token, http_method='GET', http_url=url,
parameters=parameters)
#below is the main function galls!!
oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
consumer, token)
return oauth_request.to_url()
def oauth():
return {"consumer_key": "2HZq407wF.................",
"consumer_secret": "OsemLubDmCcQq5Y3q............",
"token_key": "75230340-2SGPJWWn..............",
"token_secret": "NZcII332Y3EI.............."}
我一直在寻找解决方案,其中大多数似乎都使用urllib2作为CURL,我认为这与python 3不兼容?
你有什么建议吗?我被困在这一步,哪儿也不去
谢谢你,亚尔