• Requests 乱码


    当使用Requests请求网页时,出现下面图片中的一些乱码,我就一脸蒙逼。

    程序是这样的。

    def getLinks(articleUrl):
        headers = {
            "Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
            }
        wb_data = requests.get(articleUrl,headers=headers)
        bsObj = BeautifulSoup(wb_data.text,"lxml")
        return bsObj

    程序的中出现的乱码图片是这样的。

    怎么解决呢?好在有google大神,让我找到了一些前辈写的博客,拿去看吧,^_^。

    http://blog.chinaunix.net/uid-13869856-id-5747417.html

    http://blog.csdn.net/a491057947/article/details/47292923#t1

    还有官网链接。两个地方都有讲到。(偷偷告诉你有chinese版本的,自己去找吧)

    http://docs.python-requests.org/en/latest/user/quickstart/#response-content

    http://docs.python-requests.org/en/master/user/advanced/#compliance

    英文不好,我们来看看中文版的说的是什么,见下图。

    好了,资料看完了,总结一下吧。

    解决思路:

    1.见到有乱码,不用怕,首先我们来看看编码方式是什么?怎么看?把编码方式打印出来看看。

    def getLinks(articleUrl):
        headers = {
            "Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
            }
        wb_data = requests.get(articleUrl,headers=headers)
        bsObj = BeautifulSoup(wb_data.text,"lxml")
        hrefs = bsObj.find("div",{"class":"booklist clearfix"})
        print(wb_data.headers['content-type'])
        print(wb_data.encoding) # response的内容编码
        print(wb_data.apparent_encoding) #response headers 里设置的编码
        print(requests.utils.get_encodings_from_content(wb_data.text)) #response返回的html header标签里设置的编码
        return bsObj

    返回的是这些个鬼东西。

    text/html
    ISO-8859-1  # response的内容编码
    UTF-8-SIG   #response headers 里设置的编码
    ['utf-8']   #response返回的html header标签里设置的编码

    这下知道为啥乱码了,原来是response的内容编码和response headers 里设置的编码不一样啊。

    2.怎么办呢?不一样,那我们就改成一样的。改变response的内容编码格式。

    有两种方法:

    (1)使用.encoding属性改变response的内容编码,在代码里加上下面一行代码。

    wb_data.encoding = 'utf-8' #手动指定编码方式
    def getLinks(articleUrl):
        headers = {
            "Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
            }
        wb_data = requests.get(articleUrl,headers=headers)
        wb_data.encoding = 'utf-8' #手动指定编码方式
        bsObj = BeautifulSoup(wb_data.text,"lxml")
        return bsObj

    (2)使用原始的Response.content

    bsObj = BeautifulSoup(wb_data.text,"lxml")
    #将wb_data.text改为wb_data.content
    bsObj = BeautifulSoup(wb_data.content,"lxml") 

    3.从前面链接里就可以看到,一位前辈写出了下面代码。解决这类问题,一劳永逸的方法。
    我给应用到我的代码里,看看可行不?^_^。

    原理是这样的,当response内容的编码是'ISO-8859-1',首先查找返回的Html的header标签里设置的编码;如果此编码不存在,查看response header设置的编码

    def getLinks(articleUrl):
        headers = {
            "Uset-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.1.0.14126"
            }
        wb_data = requests.get(articleUrl,headers=headers)
    
        if wb_data.encoding == 'ISO-8859-1':
            encodings = requests.utils.get_encodings_from_content(wb_data.text)
            if encodings:
                encoding = encodings[0]
            else:
                encoding = wb_data.apparent_encoding
        encode_content = wb_data.content.decode(encoding,'replace').encode('utf-8','replace')
        
        bsObj = BeautifulSoup(encode_content,"lxml")    
        return bsObj

    好了,这下就能解决这个问题了。哎,这个小鬼挺能折腾的。

      

  • 相关阅读:
    io系列之常用流一
    C++ 函数参数的默认值
    C++ 函数匹配和作用域声明
    c++ vector 迭代器 demo
    C++ 函数重载和匹配
    C++函数重载和const
    C++函数重载
    iOS开源项目
    Linux系统/网络 笔记
    IO五种模式
  • 原文地址:https://www.cnblogs.com/billyzh/p/6148066.html
Copyright © 2020-2023  润新知