我正在尝试使用Gnip Historical 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 -uname -d '{"title":"HPT_test_job","publisher":"Twitter","streamType":"track","dataFormat":"activity-streams","fromDate":"201401010000","toDate":"201401020000 ","rules":{"value":"twitter_lang:en (希拉里·克林顿或唐纳德)“,"tag":"2014_01_01_snow"}}‘'https://historical.gnip.com/accounts/account_name/jobs.json’
这是确切的输出msg:
检索作业状态时出错:{u‘’serviceUsername‘:u’‘is invalid'} --请验证连接参数和网络连接*
发布于 2015-09-28 15:27:42
试试这个..。看看有没有帮助
import urllib2
from urllib2.request import urlopen
u = urlopen ('http:// .........')发布于 2017-03-14 10:39:21
如果你使用的是Python3.5,你应该使用库urllib.request,它是urllib2的更新版本。但是,请注意,这会更改代码中的一些内容,包括print (应该放在括号中)和需要将一些字符串结果转换为字节。Here您可以查看适用于Python3.5的所有必需的代码更改
https://stackoverflow.com/questions/32817158
复制相似问题