阿神2017-04-18 10:00:42
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
url = 'http://nj.lianjia.com/xiaoqu/'
html = requests.get(url)
soup = BeautifulSoup(html.text.encode(html.encoding), 'lxml', from_encoding='utf-8')
title = soup.title.get_text()
print(title)
1) 加 # -- coding: utf-8 --聲明
2) 正確的處理返回的response的編碼
高洛峰2017-04-18 10:00:42
樓上沒錯,其實你也可以用Latin1
編碼來解碼這段文字。
import requests
from bs4 import BeautifulSoup
url = 'http://nj.lianjia.com/xiaoqu/'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'lxml')
title = soup.title.get_text()
print(title.encode('latin1').decode('utf-8'))