鍍金池/ 問(wèn)答/Python/ 爬蟲(chóng)時(shí)網(wǎng)頁(yè)顯示的正常中文,源代碼卻亂了

爬蟲(chóng)時(shí)網(wǎng)頁(yè)顯示的正常中文,源代碼卻亂了

初學(xué)python不久,在用selenium爬實(shí)習(xí)僧的網(wǎng)站時(shí)(搜索奔馳),再用開(kāi)發(fā)者工具看源代碼,是這樣的
圖片描述

源網(wǎng)頁(yè)是正常的:
clipboard.png

讀取這個(gè)網(wǎng)頁(yè)的代碼是這樣的(小部分):

def get_products():

"""
提取商品數(shù)據(jù)
"""
#page_source屬于str格式
html = browser.page_source
doc = pq(html)
items = doc('.position .position-list li.font').items()
for item in items:
    product = {
        'name': item.find('.name').text(),
        'release_time': item.find('.release-time').text(),
        'company': item.find('.company').text(),
        'area': item.find('.area').text(),
        'info': item.find('.more').text(),
    }
    print(product)
    

然后在spyder(用的anaconda3)的控制臺(tái)中的輸出是這樣的,其中上面截圖對(duì)應(yīng)的是‘info’的信息

{'name': 'ue222uee04uf627 uf627uee14uee14uebe3ue321ue817 實(shí)習(xí)ue194', 'release_time': '2天前', 'company': '戴姆勒奔馳', 'area': '北京', 'info': 'ue83buf591uf591-ue83buf825uf591/天|uf825天/周|uecb6個(gè)月'}

之后寫(xiě)入txt文件,用了utf8編碼,發(fā)現(xiàn)還是一個(gè)樣子。
代碼:

def save_to_text(product):

file = word + '.txt'
with open(file, 'a' , encoding='utf-8') as k:
    for key, value in product.items():
        k.write(key + ':' + value + '\n')

打開(kāi)文件:
name:?? ???? 實(shí)習(xí)
release_time:2天前
company:戴姆勒奔馳
area:北京
info:??-??/天|?天/周|?個(gè)月

所以到底還是編碼的問(wèn)題么?

回答
編輯回答
耍太極

反爬了,你如果只是想練習(xí)的話換個(gè)網(wǎng)站爬

2017年6月4日 09:29
編輯回答
艷骨

字體反爬蟲(chóng),需要單獨(dú)解析字體

2018年5月23日 00:19
編輯回答
心癌

嘗試獲取網(wǎng)頁(yè)的編碼 然后對(duì)結(jié)果進(jìn)行解碼在重新編碼

url = 'http://www.cea.gov.cn/publish...'
result = requests.get(url=url)
print(result.encoding) # <-- 獲取網(wǎng)頁(yè)編碼格式

2017年2月13日 12:10