【问题标题】:How to get unicode string when extract data in Python?在 Python 中提取数据时如何获取 unicode 字符串?
【发布时间】:2015-09-20 02:12:54
【问题描述】:

我正在尝试从越南网站中提取文本,该网站的字符集为 utf-8。但是,我得到的文本总是在 Ascii 中,我找不到将它们转换为 unicode 或准确获取网站上的文本的方法。结果,我无法按预期将它们保存到文件中。
我知道这是 Python 中 unicode 非常流行的问题,但我仍然希望有人能帮助我解决这个问题。谢谢。
我的代码:

import requests, re, io
import simplejson as json
from lxml import html, etree

base = "http://www.amthuc365.vn/cong-thuc/"
page = requests.get(base + "trang-" + str(1) + ".html")
pageTree = html.fromstring(page.text)

links = pageTree.xpath('//ul[contains(@class, "mt30")]/li/a/@href')
names = pageTree.xpath('//h3[@class="title"]/a/text()')
for name in names[:1]:
    print name
    # Làm bánh oreo nhân bÆ¡ Äậu phá»ng thÆ¡m bùi

但我需要的是“Làm bánh oreo nhân bơ đậu phộng thơm bùi”
谢谢。

【问题讨论】:

    标签: python unicode web-scraping


    【解决方案1】:

    只需从 page.text 切换到 page.content 就可以了。

    解释here

    另见:

    【讨论】:

    • 非常感谢@alecxe
    猜你喜欢
    • 1970-01-01
    • 2014-05-15
    • 2011-12-24
    • 2011-05-13
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多