Python的UTF8编码与阿拉伯语

时间:2012-12-22 22:33:06

标签: python django utf-8

我遇到编码问题,当我尝试抓取youtube(阿拉伯语频道)时:

#!/usr/bin/python
# -*- coding: utf8 -*- 
from django.core.management.base import BaseCommand, CommandError
import requests, lxml, re
from lxml import html

class Command(BaseCommand):
    def handle(self, *args, **options):
        r = requests.get("https://www.youtube.com/user/aljazeerachannel/videos?view=0")
        root = lxml.html.fromstring(r.content)

        for data in root.xpath('.//*[@id="branded-page-body"]/div/div/div[1]/div/div[2]/ul/li[1]/span/span/a'):
            print data.text

结果是:

[root@vmi9105 buzzbal]# python manage.py youtube

        اÙتخابات اÙÙجاÙس اÙبÙدÙØ© Ù٠سÙØ·ÙØ© عÙÙاÙ

1 个答案:

答案 0 :(得分:2)

试试这个它解决了我在 python 中的问题:

f"{yourString}".encode('latin-1').decode("utf-8")