我正在尝试使用Gnip Historical Powertrack API创建作业。
我遇到了urllib的问题..
func theFunctionWhereTheHeightChanges() {
// change the height
heightDidChange?()
}
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()
当我尝试在终端中运行下面的一个时,我收到错误 - ServiceUsername无效。
curl -v -X POST -uname -d'{“title”:“HPT_test_job”,“publisher”:“Twitter”,“streamType”:“track”,“dataFormat”:“activity-streams”,“ fromDate“:”201401010000“,”toDate“:”201401020000“,”规则“:[{”value“:”twitter_lang:en(Hillary Clinton OR Donald)“,”tag“:”2014_01_01_snow“}]}''{ {3}}“
检索作业状态时出错:{u'serviceUsername':[u'is invalid']} - 请验证您的连接参数和网络连接*
答案 0 :(得分:0)
试试这个..看看是否有帮助
import urllib2
from urllib2.request import urlopen
u = urlopen ('http:// .........')
答案 1 :(得分:0)
如果您使用的是python 3.5,则应使用urllib2的更新版本urllib.request库。但请注意,这会改变代码中的一些内容,包括print(应该在括号中)以及需要将一些字符串结果转换为字节。 Here您可以查看适用于python 3.5的代码中所需的所有更改