Python輸出 u編碼將其轉換成中文

2021-07-26 12:02:42 字數 1250 閱讀 2601

整個**demo

# -*- coding: utf-8 -*-

#小豬短租爬取

import requests

from bs4 import beautifulsoup

import json

defget_xinxi

(i):

url = '' %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=

print(json.dumps(data).decode("unicode-escape"))

i=1while(i<12):

get_xinxi(i)

i=i+1

爬取了12頁的資訊

小結:

壓注意的是

建立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編碼將其轉換成中文的例項

了下。python2.7在window上的編碼確實是個坑 解決如下 如果是個字典的話要先將其轉成字串 匯入json庫 然後 這麼輸出 json.dumps data decode unicode escape 整個 demo coding utf 8 小豬短租爬取 import requests f...

Python 編碼轉換

coding utf 8 s abc print type s str utf 8 print len s 3 s unicode s str unicode,其中str的每個字元值必須小於128 print type s unicode print len s 3 s u abc print ty...

python編碼轉換

參見 主要介紹了python的編碼機制,unicode,utf 8,utf 16,gbk,gb2312,iso 8859 1 等編碼之間的轉換。常見的編碼轉換分為以下幾種情況 1.自動識別字串編碼 coding utf8 import urllib import chardet rawdata ur...