0

我正在尝试使用 Gnip Historical Powertrack API 创建工作。

我遇到了 urllib 的问题。

import urllib2  
import base64  
import json  
UN = '' # YOUR GNIP ACCOUNT EMAIL ID  
PWD = ''  
account = '' # YOUR GNIP ACCOUNT USER NAME  
def get_json(data):  
    return json.loads(data.strip())  
def post():  
    url = 'https://historical.gnip.com/accounts/' + account + '/jobs.json'  
    publisher = "twitter"  
    streamType = "track"  
    dataFormat = "activity-streams"  
    fromDate = "201510140630"  
    toDate = "201510140631"  
    jobTitle = "job30"  
    rules = '[{"value":"","tag":""}]'  
    jobString = '{"publisher":"' + publisher + '","streamType":"' + streamType + '","dataFormat":"' + dataFormat + '","fromDate":"' + fromDate + '","toDate":"' + toDate + '","title":"' + jobTitle + '","rules":' + rules + '}'  
    base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '')  
    req = urllib2.Request(url=url, data=jobString)  
    req.add_header('Content-type', 'application/json')  
    req.add_header("Authorization", "Basic %s" % base64string)  

    proxy = urllib2.ProxyHandler({'http': 'http://proxy:8080', 'https': 'https://proxy:8080'})  
    opener = urllib2.build_opener(proxy)  
    urllib2.install_opener(opener)  
    try:  
        response = urllib2.urlopen(req)  
        the_page = response.read()  
        the_page = get_json(the_page)  
        print 'Job has been created.'  
        print 'Job UUID : ' + the_page['jobURL'].split("/")[-1].split(".")[0]  
    except urllib2.HTTPError as e:  
        print e.read()  

if __name__=='__main__':  
    post()  

这是我得到的错误:

Traceback (most recent call last):
  File "gnip1.py", line 37, in <module>
    post()  
  File "gnip1.py", line 28, in post
    response = urllib2.urlopen(req)  
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>

我什至尝试过 curl 命令:

当我尝试在终端中运行以下命令时,出现错误 - ServiceUsername 无效。

curl -v -X POST -uname -d '{"title":"HPT_test_job","publisher":"Twitter","streamType":"track","dataFormat":"activity-streams","fromDate": "201401010000","toDate":"201401020000","rules":[{"value": "twitter_lang:en (Hillary Clinton OR Donald)","tag": "2014_01_01_snow"}]}'' https:// history.gnip.com/accounts/account_name/jobs.json '

这是确切的输出消息:

检索作业状态时出错:{u'serviceUsername': [u'is invalid']} -- 请验证您的连接参数和网络连接 *

4

2 回答 2

0

如果您使用的是 python 3.5,您应该使用库 urllib.request,它是 urllib2 的较新版本。但是请注意,这会更改代码中的一些内容,包括 print(应该在括号中)以及需要将一些字符串结果转换为字节。在这里,您可以查看适用于 python 3.5 的代码中所有必需的更改

于 2017-03-14T02:39:21.367 回答
0

试试这个..看看它是否有帮助

import urllib2
from urllib2.request import urlopen 
u = urlopen ('http:// .........')
于 2015-09-28T07:27:42.493 回答