BeautifulSoup獲取網頁爲亂碼的解決

1、扒取網頁

        req = requests.get(url = target)
        html = req.text
        bf = BeautifulSoup(html,'html.parser')
        texts = bf.find_all('a')
        print(texts)

 2、輸出結果

 

 

 3、F12查看原頁面

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

4、估計是默認使用的編碼不對,頁面內容取回後先指定解碼方式,試試。

代碼:

        req = requests.get(url = target)
        req.encoding='utf-8' #指定編碼格式,解決中文亂碼顯示
        html = req.text
        bf = BeautifulSoup(html,'html.parser')
        texts = bf.find_all('a')
        print(texts)

結果:

 

 

5、問題解決

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章