【问题标题】:Transforming Curl into Python using Urllib with Sentiment140 API使用带有 Sentiment140 API 的 Urllib 将 Curl 转换为 Python
【发布时间】:2013-07-09 09:43:20
【问题描述】:

我正在尝试使用 Python 从 Sentiment140 API 请求数据。 API 使用批量分类服务 (JSON)。在终端内它工作正常

curl -d "{'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}" http://www.sentiment140.com/api/bulkClassifyJson

导致以下响应:

{"data":[{"text":"I love Titanic.","polarity":4,"meta":{"language":"en"}},{"text":"I hate Titanic.","polarity":0,"meta":{"language":"en"}}]}

我认为我可以使用 urllib 从我的 python 代码中获得相同的响应。我试过了:

import urllib
import urllib2

url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}

data = urllib.urlencode(values)
response = urllib2.urlopen(url, data)
page = response.read()

代码有效,但没有给我任何结果。 我错过了什么吗?

【问题讨论】:

    标签: python json curl urllib2 sentiment-analysis


    【解决方案1】:

    我认为你需要在这里使用json。

    尝试做:

    data = json.dumps(values) # instead of urllib.urlencode(values)
    response = urllib2.urlopen(url, data)
    page = response.read()
    

    在顶部

    import json 
    

    【讨论】:

      猜你喜欢
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      相关资源
      最近更新 更多