第一次寫貼子,試試水
很多玩Python3的都會遇到編碼問題,如果直接去處理未知編碼的網頁,不是Python內部編碼格式utf8會出現亂碼,下面介紹一種將未知編碼的字串轉換為utf8來避免亂碼的方法,
在很多Python編碼轉換的場景中都可以使用,
這段是自己寫的爬蟲中的一段代碼,代碼比較簡短,聰明的你一定能抓住其中的重點
#請求網頁並轉網頁編碼 def getHtmlAndDealCode(url): #html=requests.get(url,verify=False) html = s.get(url,headers=header) code=html.encoding html=html.text html=html.encode(code) html=html.decode('utf-8') parser = 'html.parser' soup = BeautifulSoup(html ,parser) return soup