本文將根據實踐經驗說明python中使用requests庫編寫爬蟲程序時,出現【中文亂碼】的原因及解決辦法。
首先,本文的【中文亂碼】情況,指的是原網頁中的中文內容在使用requests獲取后,中文完全無法識別的情況,區別于\x、\u等編碼情況。如下圖中的例子:
導致上圖中【中文亂碼】的原因:
使用requests庫時,選擇使用的文本響應方法不合適,且沒有在代碼中添加設置合適的編碼,以致于使用【response.text】自動獲取到的網頁編碼,與實際網頁的編碼不一致,進而產生【中文亂碼】。
使用requests庫時,可能已經形成了一個習慣,常用【response.text】進行文本響應,而【response.content】常用于圖片、視頻等。
這兩者,最大的一個區別就是:
1、【response.text】會自動根據HTTP頭部去推測網頁的編碼,解碼并返回解碼后的文本。
2、【response.content】不會解碼,直接以二進制形式返回。
兩種文本響應方法,如下表:
response.text | 服務器響應的內容,會自動根據響應頭部的字符編碼進行解碼。根據HTTP頭部對響應的編碼做出有根據的推測,推測文本編碼。返回類型:str;常用于:響應文本 |
response.content | 字節方式的響應體,不會根據HTTP頭部對響應的編碼做出有根據的推測。返回類型:bytes(二進制);常用于:圖片、視頻 |
最有效的解決方法:
使用response的encoding、apparent_encoding,得到網頁編碼。
encoding、apparent_encoding兩者最大的區別:
encoding是從header中去提取,而apparent_encoding是從網頁源碼去解析,apparent_encoding得到的結果更準確。
詳細如下表:
response.encoding | 從網頁響應的header中,提取charset字段中的編碼。若header中沒有charset字段,則默認為ISO-8859-1編碼模式,ISO-8859-1編碼無法解析中文,這也是中文亂碼的原因。 |
response.apparent_encoding | 從網頁的內容中(html源碼)中分析網頁編碼的方式。所以apparent_encoding比encoding更加準確,獲取到的才是原網頁的實際編碼。 |
print(response.apparent_encoding)
print(response.encoding)
使用encoding、apparent_encoding兩種方法,所得的結果是不一致的,apparent_encoding才是原網頁實際編碼。如下圖
根據上述方法,獲得原網頁的實際編碼后,手動在代碼中指定文本編碼格式,即可解決【中文亂碼】問題。如下圖:
response.encoding=response.apparent_encoding
以上就是使用requests爬蟲解決中文亂碼的方法,如那位老師有更好的方法還望賜教,謝謝!
clipse運行頁面顯示中文亂碼
頁面源碼
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="ISO-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>客戶列表-BootCRM</title>
</head>
<body>
<h1>解決中文亂碼</h1>
</body>
</html>
tomcat運行后
打開瀏覽器地址欄打就會出現亂碼現象
分析問題
首先排查一下服務器是否啟動,回看console最下面 server startup 表示服務器已經啟動了 沒有問題也沒有報錯。
其次查看頁面源碼,發現有三處編碼為ISO-8859-1。
注意:數字英文都正常顯示,只是中文亂碼,是因為ISO-8859-1是單字節編碼,此字符集支持部分于歐洲使用的語言,這個編碼不支持中文,所以要換支持的編碼啦,現在一般都通用UTF-8,因為ISO-8859-1是一個8位的容器。因為只有8位, 沒那么多地方可以表示中文,但是,由于是單字節編碼,和計算機最基礎的表示單位一致,所以很多時候,仍舊使用 ISO-8859-1編碼來表示。而且在很多協議上,默認使用該編碼。
解決辦法
將代碼中三處的ISO-8859-1 改成UTF-8就好了。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>客戶列表-BootCRM</title>
</head>
<body>
<h1>解決中文亂碼</h1>
</body>
</html>
此時運行結果就正常了
要使用的是wkhtmltopdf的Python封裝——pdfkit
安裝
1. Install python-pdfkit:
$ pip install pdfkit
2. Install wkhtmltopdf:
$ sudo apt-get install wkhtmltopdf
sudo yum intsall wkhtmltopdf
brew install Caskroom/cask/wkhtmltopdf
使用
一個簡單的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以傳遞一個url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以傳遞一個打開的文件:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
如果你想對生成的PDF作進一步處理, 你可以將其讀取到一個變量中:
# 設置輸出文件為False,將結果賦給一個變量
pdf = pdfkit.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 選項 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除選項名字前面的 '--' .如果選項沒有值, 使用None, False or * 作為字典值:
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默認情況下, PDFKit 將會顯示所有的 wkhtmltopdf 輸出. 如果你不想看到這些信息,你需要傳遞一個 quiet 選項:
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令語法 , TOC 和 Cover 選項必須分開指定:
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
當你轉換文件、或字符串的時候,你可以通過css選項指定擴展的 CSS 文件。
# 單個 CSS 文件
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css)
你也可以通過你的HTML中的meta tags傳遞任意選項:
body = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape
配置
每個API調用都有一個可選的參數。這應該是pdfkit.configuration()API 調用的一個實例. 采用configuration 選項作為初始化參數。可用的選項有:
示例 :針對wkhtmltopdf不在系統路徑中(不在$PATH里面)
PATH里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
問題
IOError:'No wkhtmltopdf executable found':
確保 wkhtmltopdf 在你的系統路徑中(PATH), 會通過 configuration進行了配置 (詳情看上文描述)。 在Windows系統中使用where wkhtmltopdf命令 或 在 linux系統中使用 which wkhtmltopdf 會返回 wkhtmltopdf二進制可執行文件所在的確切位置.
如果出現這個錯誤意味著 PDFKit不能處理一個輸入。你可以嘗試直接在錯誤信息后面直接運行一個命令來查看是什么導致了這個錯誤 (某些版本的 wkhtmltopdf會因為段錯誤導致處理失?。?/p>
確保兩項:
1)、你的系統中有中文字體
2)、在html中加入
下面是我隨便寫的一個HTML表格:
<html>
<head><meta charset="UTF-8"></head>
<body>
<table width="400" border="1">
<tr>
<th align="left">Item....</th>
<th align="right">1</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">1.10</td>
</tr>
<tr>
<td align="left">化妝品</td>
<td align="right">.00</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">0.40</td>
</tr>
<tr>
<th align="left">tOTAL</th>
<th align="right">01.50</th>
</tr>
</table>
</body>
</html>
下面是生成的PDF截圖
*請認真填寫需求信息,我們會在24小時內與您取得聯系。