【问题标题】:Writing on text file, accents and special characters not displaying correctly在文本文件上书写、重音符号和特殊字符无法正确显示
【发布时间】:2016-02-19 02:56:12
【问题描述】:

这就是我正在做的,我在一个网站上进行网络爬取供我个人使用,以复制文本并将一本书的章节放在文本格式上,然后用另一个程序自动将其转换为 pdf 以将其放入我的云在发生这种情况之前一切都很好:特殊字符没有正确复制,例如重音在文本文件上显示为:\xe2\x80\x99,而 - 显示为 \xe2\x80\x93。我用过这个(Python 3):

    for text in soup.find_all('p'):
        texta = text.text
        f.write(str(str(texta).encode("utf-8")))
        f.write('\n')

因为我在读取这些字符时遇到了一个错误并且它刚刚停止了我的程序,所以我将所有内容编码为 utf-8 并使用 python 的方法 str() 将所有内容重新转换为字符串

如果有人对我的问题有更好的解决方案,我会发布整个代码,这里是从第 1 页到 max_pages 爬取网站的部分,您可以在第 21 行修改它以获得更多或更少的书章:

import requests

from bs4 import BeautifulSoup

def crawl_ATG(max_pages):
    page = 1
    while page <= max_pages:
        x= page
        url = 'http://www.wuxiaworld.com/atg-index/atg-chapter-' + str(x) + "/"
        source = requests.get(url)
        chapter = source.content
        soup = BeautifulSoup(chapter.decode('utf-8', 'ignore'), 'html.parser')
        f = open('atg_chapter' + str(x) + '.txt', 'w+')
        for text in soup.find_all('p'):
        texta = text.text
            f.write(str(str(texta).encode("utf-8")))
            f.write('\n')
        f.close
        page +=1
    
crawl_ATG(10)

当我得到这个问题的解决方案时,我会清理第一批复制的无用行。谢谢

【问题讨论】:

  • 您使用的是 Python 2 还是 Python 3?这很重要。阅读the Python Unicode howto.
  • 我正在使用 Python 3,感谢您提供的链接,我将仔细研究它。 @BobDylan
  • 无论您使用的是 Python 2 还是 Python 3,这都很重要,因为 str() 的含义完全不同。您需要编辑此问题以说明哪些人可以帮助您

标签: python encoding utf-8 web-crawler utf


【解决方案1】:

我发现解决这个问题的最简单方法是在 open 函数中添加encoding= "utf-8"

with open('file.txt','w',encoding='utf-8') as file :
   file.write('ñoño')

【讨论】:

    【解决方案2】:

    我能发现的唯一错误是,

    str(texta).encode("utf-8")
    

    在其中,您强制转换为 str 并对其进行编码。它应该替换为,

    texta.encode("utf-8")
    

    编辑:

    错误源于服务器没有为页面提供正确的编码。所以requests 假定为'ISO-8859-1'。正如bug 中所述,这是一个深思熟虑的决定。

    幸运的是,chardet 库可以正确检测到'utf-8' 编码,因此您可以这样做:

    source.encoding = source.apparent_encoding
    chapter = source.text
    

    并且不需要手动解码chapter 中的文本,因为requests 使用它来为您解码content

    【讨论】:

    • 这是多余的,谢谢你,但它仍然没有在我的文本文件上写入特殊字符:(
    【解决方案3】:

    由于某种原因,您(错误地)在 Python3 字符串中有 utf8 编码数据。真正的原因可能是requests.content已经是一个unicode字符串,所以你不应该解码它,而是直接使用它:

        url = 'http://www.wuxiaworld.com/atg-index/atg-chapter-' + str(x) + "/"
        source = requests.get(url)
        chapter = source.content
        soup = BeautifulSoup(chapter, 'html.parser')
    

    如果还不够,那就意味着如果你还有'和-(Unicodeu'\u2019'u'\u2013')显示为\xe2\x80\x99\xe2\x80\x93',这可能是由于html页面没有正确声明它编码。在这种情况下,您应该首先使用 latin1 编码编码为字节字符串,然后将其解码为 utf8:

    chapter = source.content.encode('latin1', 'ignore').decode('utf8', 'ignore')
    soup = BeautifulSoup(chapter, 'html.parser')
    

    演示:

    t = u'\xe2\x80\x99 \xe2\x80\x93'
    t = t.encode('latin1').decode('utf8')
    

    显示:u'\u2019 \u2013'

    print(t)
    

    显示:’ –

    【讨论】:

    • 说字节内容没有编码是行不通的。我在互联网上查找了一些网站,发现我的文本文档的每一行都是 Bynary Literal。我将让我的代码保持原样,并制作一个从 Bynary Literal 到 String 的转换器
    猜你喜欢
    • 2016-12-21
    • 2014-01-04
    • 2012-05-13
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多