[問題] 新手請教Beautiful Soup執行環境

看板Python作者 (fealing)時間8年前 (2017/03/31 15:41), 編輯推噓0(005)
留言5則, 3人參與, 最新討論串1/1
各位版友大家好 最近再學習爬蟲,目前正在試著用 Beautiful Soup抓資料 如果我把下面的程式碼在virtual environment 或是 打開cmd,切到python目錄下執行,都會有問題 =============================================== import requests from bs4 import BeautifulSoup res = requests.get('http://www.google.com') soup = BeautifulSoup(res.text,'html.parser') =============================================== 錯誤訊息: UnicodeEncodeError: 'charmap' codec can't encode characters 可是如果我開啟python schell 一行一行把code寫進去,卻是沒有問題的 (也可以抓到我想要的資料) 請問各位版友,可以幫忙告訴我是什麼問題嗎? google了好久都沒有頭緒 非常謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.157.49 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1490946101.A.31B.html

03/31 17:33, , 1F
windows? 把 .text 改用 .content 試試
03/31 17:33, 1F

04/02 00:44, , 2F
cmd編碼跟python編碼好像不一樣
04/02 00:44, 2F

04/02 00:44, , 3F
不知道是不是這個原因
04/02 00:44, 3F

04/02 09:21, , 4F
requests.encoding 看一下編碼,預設是 ISO-8859-1
04/02 09:21, 4F

04/02 09:22, , 5F
^ res.encoding
04/02 09:22, 5F
文章代碼(AID): #1OtWWrCR (Python)
文章代碼(AID): #1OtWWrCR (Python)