Python輸出\u編碼將其轉換成中文的實例
爬取了下小豬短租的網(wǎng)站出租房信息但是輸出的時候是這種:
百度了下。python2.7在window上的編碼確實是個坑
解決如下
如果是個字典的話要先將其轉成字符串 導入json庫
然后 這么輸出(json.dumps(data).decode("unicode-escape"))
整個代碼demo
# -*- coding: UTF-8 -*- #小豬短租爬取 import requests from bs4 import BeautifulSoup import json def get_xinxi(i): url = 'http://cd.xiaozhu.com/search-duanzufang-p%d-0/' %i html = requests.get(url) soup = BeautifulSoup(html.content) #獲取地址 dizhis=soup.select(' div > a > span') #獲取價格 prices = soup.select(' span.result_price') #獲取簡單信息 ems = soup.select(' div > em') datas =[] for dizhi,price,em in zip(dizhis,prices,ems): data={ '價格':price.get_text(), '信息':em.get_text().replace('\n','').replace(' ',''), '地址':dizhi.get_text() } print(json.dumps(data).decode("unicode-escape")) i=1 while(i<12): get_xinxi(i) i=i+1
爬取了12頁的信息
小結:
壓注意的是
創(chuàng)建soup
soup = BeautifulSoup(html.content)
多個值的for賦值
for dizhi,price,em in zip(dizhis,prices,ems):
字典的輸出編碼問題
json.dumps(data).decode("unicode-escape")
如果想獲取每個個詳細信息可以獲取其href屬性值
#page_list > ul > li:nth-of-type(1) > a
然后獲取其屬性值get(‘href')獲取每個的詳情信息在解析頁面獲取想要的信息加在data字典中
以上這篇Python輸出\u編碼將其轉換成中文的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Django?ORM數(shù)據(jù)庫操作Python化藝術探索
這篇文章主要介紹了Django?ORM數(shù)據(jù)庫操作Python化藝術探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Python3調用百度AI識別圖片中的文字功能示例【測試可用】
這篇文章主要介紹了Python3調用百度AI識別圖片中的文字功能,結合實例形式分析了Python3安裝及使用百度AI接口的相關操作技巧,并附帶說明了百度官方AI平臺的注冊及接口調用操作方法,需要的朋友可以參考下2019-03-03