2018-10-23 09:55:46 補充
import requests
response = requests.get("www.baidu.com",timeout=10)
coding = response.apparent_encoding # GB2312 或者 其他
# 或者 從html的meta中抽取
# coding = requests.utils.get_encodings_from_content(response.text)[0]
response.encoding = coding
print(response.text)
2018-10-10 10:33:15 添加
import requests
from bs4 import BeautifulSoup
response = requests.get("www.baidu.com",timeout=10)
if response:
soup = BeautifulSoup(response.text, 'lxml')
try: # 自動轉(zhuǎn)碼為網(wǎng)頁需要的編碼
meta = soup.meta.attrs
meta = meta['content'].split("=")[1] # 獲取編碼
response.encoding = meta # Requests庫的自身編碼為: r.encoding = ‘ISO-8859-1’
response = response.text
print(response)
except BaseException as e:
pass
# 參照地址: https://blog.csdn.net/chaowanghn/article/details/54889835
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者