Twitter搜索API:Python中的HTTPError(req.full_url,code,msg,hdrs,fp)

时间:2019-07-16 15:00:48

标签: python api twitter urllib http-error

我目前正在尝试访问Twitter搜索API,但在此问题上绊绊,无法解决。 我的python程序不断告诉我,我出现了 HTTPError(req.full_url,代码,msg,hdrs,fp)

错误

下面的程序可以在Twitter's standard search API上很好地工作,但是当我在Twitter's free premium search API上尝试它时,我不断收到该错误消息。

我已经尝试寻找一种方法来解决此问题,例如通过处理mod_security,解码utf-8等,但到目前为止仍无法正常工作。

import urllib.request, urllib.parse, urllib.error
import oauth
import ssl
import json

def oauthh():
    return {"consumer_key": "gmW4b2YiWo.................",
            "consumer_secret": "S9c5J3O.................",
            "token_key": "75230...........................",
            "token_secret": "OU............."}

def augment(url, parameters):
    secrets = oauthh()
    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=token, http_method='GET', http_url=url,
                    parameters=parameters)
    oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(),
                               consumer, token)
    return oauth_request.to_url()


if __name__ == "__main__":

    '''
    class AppURLopener(urllib.request.FancyURLopener):
        version = "Mozilla/5.0"

    opener = AppURLopener()
    response = opener.open('http://httpbin.org/user-agent')
    '''

    TWITTER_URL = 'https://api.twitter.com/1.1/tweets/search/30day/data1.json?' #premium search
    #TWITTER_URL = 'https://api.twitter.com/1.1/search/tweets.json?' #standard search

    parameters = {'q': 'sbux', 
                  'query':'pantat',
                  'lang':'id',
                  'fromDate':'201907100000',
                  'toDate':'201907120000',
                  'count': '5',
                  'maxResults': '100',                 
                 }

    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE

    while True:  
        url = augment(TWITTER_URL, parameters)
        print('\nRetrieving', url, '\n')

        #url = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
        connection = urllib.request.urlopen(url, context=ctx)
        data = connection.read().decode('utf-8')

        js = json.loads(data)
        jsdmp = json.dumps(js, indent=2)

        print(jsdmp)
        break

你知道为什么吗? 谢谢

0 个答案:

没有答案