【问题标题】:Trying to get encoding from a webpage Python and BeautifulSoup试图从网页 Python 和 BeautifulSoup 获取编码
【发布时间】:2013-08-21 13:39:12
【问题描述】:

我正在尝试从网页中检索字符集(这将一直更改)。目前我使用 beautifulSoup 解析页面,然后从标题中提取字符集。在我遇到一个具有.....

的网站之前,这一切正常
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

到目前为止,我与其他页面一起使用的代码是:

    def get_encoding(soup):
        encod = soup.meta.get('charset')
        if encod == None:
            encod = soup.meta.get('content-type')
            if encod == None:
                encod = soup.meta.get('content')
    return encod

有没有人知道如何添加到此代码以从上面的示例中检索字符集。将它标记化并尝试以这种方式检索字符集是一个想法吗?在不改变整个功能的情况下你将如何去做? 现在上面的代码正在返回“text/html; charset=utf-8”,这会导致 LookupError,因为这是一个未知的编码。

谢谢

我最终使用的最终代码:

    def get_encoding(soup):
        encod = soup.meta.get('charset')
        if encod == None:
            encod = soup.meta.get('content-type')
            if encod == None:
                content = soup.meta.get('content')
                match = re.search('charset=(.*)', content)
                if match:
                    encod = match.group(1)
                else:
                    dic_of_possible_encodings = chardet.detect(unicode(soup))
                    encod = dic_of_possible_encodings['encoding'] 
    return encod

【问题讨论】:

  • 我使用过 chardet,但我希望 100% 准确,因此想尝试从页面本身获取编码。

标签: python character-encoding beautifulsoup webpage


【解决方案1】:
import re
def get_encoding(soup):
    if soup and soup.meta:
        encod = soup.meta.get('charset')
        if encod == None:
            encod = soup.meta.get('content-type')
            if encod == None:
                content = soup.meta.get('content')
                match = re.search('charset=(.*)', content)
                if match:
                    encod = match.group(1)
                else:
                    raise ValueError('unable to find encoding')
    else:
        raise ValueError('unable to find encoding')
    return encod

【讨论】:

  • 太棒了。谢谢你。真的需要自己学习一些正则表达式。
【解决方案2】:

在我的情况下,soup.meta 只返回在汤中找到的第一个 meta-tag。这是@Fruit 的答案,可以在给定的html 内的任何meta-tag 中找到charset

from bs4 import BeautifulSoup
import re

def get_encoding(soup):
    encoding = None
    if soup:
        for meta_tag in soup.find_all("meta"):
            encoding = meta_tag.get('charset')
            if encoding: break
            else:
                encoding = meta_tag.get('content-type')
                if encoding: break
                else:
                    content = meta_tag.get('content')
                    if content:
                        match = re.search('charset=(.*)', content)
                        if match:
                           encoding = match.group(1)
                           break
    if encoding:
        # cast to str if type(encoding) == bs4.element.ContentMetaAttributeValue
        return str(encoding).lower()

soup = BeautifulSoup(html)
print(get_encoding_from_meta(soup))

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2016-06-19
    • 2020-04-20
    • 2018-09-29
    • 2018-04-25
    • 2014-06-20
    相关资源
    最近更新 更多