[問題] python 爬蟲遇到big5擴充字亂碼

看板Python作者 (Jimmy)時間5年前 (2018/11/26 12:54), 5年前編輯推噓3(307)
留言10則, 6人參與, 5年前最新討論串1/1
大家好,又上來打擾了,最近在爬蟲的時候遇到這個詞: "宏碁電腦",發現爬下來時候會變成亂碼, https://imgur.com/ZSV4gAe
經爬文查詢後發現這樣的問題: https://blog.hoamon.info/2008/05/python-big5.html 不過該解法似乎不能應用在python3.7 想問一下有沒有類似的情況該怎麼解@@? 補上網站: https://tw.stock.yahoo.com/news/%E5%A4%96%E8%B3%87-%E8%B3%A3%E8%B6%85%E8%82%A1-%E5%AE%8F-%E7%A2%81-%E9%B4%BB-234706227.html 程式碼: import requests from bs4 import BeautifulSoup url='https://tw.stock.yahoo.com/news/%E5%A4%96%E8%B3%87-%E8%B3%A3%E8%B6%85%E8%82%A1-%E5%AE%8F-%E7%A2%81-%E9%B4%BB-234706227.html' req=requests.get(url) bs=BeautifulSoup(req.text,'html.parser') print(bs.find('h1').text) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.133.44.129 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1543208095.A.47E.html

11/26 13:20, 5年前 , 1F
請問是爬哪個網頁,沒有網頁沒辦法幫你看是不是網頁編
11/26 13:20, 1F

11/26 13:21, 5年前 , 2F
碼轉換上的問題,能提供程式碼更好
11/26 13:21, 2F

11/26 13:22, 5年前 , 3F
有big5哦 ,requests有encoding的參數
11/26 13:22, 3F
※ 編輯: pig98520 (220.133.44.129), 11/26/2018 13:29:53

11/26 13:39, 5年前 , 4F
req=requests.get(url)後加一行req.encoding = 'cp950'
11/26 13:39, 4F

11/26 14:55, 5年前 , 5F
可以了~不過請問為何不能用big5?
11/26 14:55, 5F

11/26 15:21, 5年前 , 6F
你貼的文章有提 有7個擴充字是big5沒有的
11/26 15:21, 6F

11/26 17:05, 5年前 , 7F
了解 但是用utf-8又會亂碼@@ 所以也不能用utf-8囉
11/26 17:05, 7F

11/26 19:35, 5年前 , 8F
用哪個 encoding 編碼,就用哪個 encoding 解碼
11/26 19:35, 8F

11/26 20:08, 5年前 , 9F
你可以去g一下CP950和Big5的關係啊
11/26 20:08, 9F

11/27 10:27, 5年前 , 10F
了解了~謝謝解惑
11/27 10:27, 10F
文章代碼(AID): #1R-toVH- (Python)