如何使用Tweepy使用Python将使用阿拉伯语文本作为查询进行流式传输?

时间:2015-05-18 20:05:20

标签: python twitter unicode streaming tweepy

我一直在尝试使用Tweepy来推送推文。虽然当查询是英文时它工作得很好,但当查询是阿拉伯语时,我无法使它工作。从以前的问题我尝试twitterStream.filter(track = [u'الإسلامية'])作为unicode,但得到了错误:

  //class variable
  int number;

    ArrayList<Integer> routeList = extras
            .getIntegerArrayList("stop_route");
    int routeListSize = routeList.size();
    //if(routeListSize > 0 && route_number == 0)
    if(routeListSize > 0){


        String[] charSequence = new String[routeList.size()];
        for (int i = 0; i<routeList.size(); i++) {
            charSequence[i] = String.valueOf(routeList.get(i));
        }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Please help us to track the route, has this route arrived the stop? ");



    builder.setMultiChoiceItems(charSequence, null,
            new DialogInterface.OnMultiChoiceClickListener() {


                public void onClick(DialogInterface dialog, int which,
                        boolean isChecked) {

                    if(isChecked){

                    }

                }
            });

        builder.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });

   }

如果在这一点上有点绝望,可以解决这个问题。

我的代码:

In[36]: twitterStream.filter(track=[u'الإسلامية'])

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3032, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-35-706cbc790f1e>", line 1, in <module>
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2978, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 3049, in run_code
    self.showtraceback()
  File "C:\Program Files (x86)\JetBrains\PyCharm 4.5\helpers\pydev\pydev_ipython_console_011.py", line 144, in showtraceback
    import traceback;traceback.print_exc()
  File "C:\Python27\lib\traceback.py", line 233, in print_exc
    print_exception(etype, value, tb, limit, file)
  File "C:\Python27\lib\traceback.py", line 125, in print_exception
    print_tb(tb, limit, file)
  File "C:\Python27\lib\traceback.py", line 70, in print_tb
    if line: _print(file, '    ' + line.strip())
  File "C:\Python27\lib\traceback.py", line 13, in _print
    file.write(str+terminator)
  File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 34-42: character maps to <undefined>

2 个答案:

答案 0 :(得分:1)

尝试使用twitterStream.filter(track=[unicode("الإسلامية", "utf-8")])

答案 1 :(得分:0)

而不是:

print status.text
return True

使用:

print('{}\t{}\t{}'.format(status.created_at,status.user.screen_name,status.text))