為什麼這個網頁的原始碼用python爬下來後用beautifulsoup解析後會出現字元丟失?

來源:互聯網
上載者:User

回複內容:

又是你這個只點感謝不點贊的壞人……
話說你為什麼要這麼心急地學呢,基礎不紮實啊,太冒進了,很顯然沒有清晰的思路…

首先編程要有預設的編碼,也就是在檔案的一開始加上
 # -*- coding: utf-8 -*-
你是python2 代碼前 加上# coding:utf-8

看python培訓黃哥的三篇文章
python開發爬蟲漢字編碼不再是問題:
將python2中漢字會出現亂碼的事一次性說清楚
article/python_bianma.md at master · pythonpeixun/article · GitHub
python爬蟲訪問多個網站、中文編碼的處理。
python爬蟲訪問多個網站、中文編碼的處理。
python3訪問sina首頁中文的處理
python3訪問sina首頁中文的處理


可能在你這個轉來轉處的地方丟失。
ur = list(ur)
ur = ur[:-1]
****************
 ur = res.contents[0]    ur = list(ur)    ur = ur[:-1]    temp = unicode(ur[0])    for index in range(0, len(ur), 4):        temp = unicode(ur[index])        print temp.strip()        print ur[index+1].string        print ur[index+1]['href']        print '\n'
似乎response 後面用了倆decode 後面一個改encode應該就好了吧。。。

Decode是把括弧裡轉成Unicode

Encode是Unicode轉成括弧裡,就是UTF-8

Unicode就是個中轉站


再不行就同一樓ignore。。。還是用Requests庫來處理http請求吧
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.