这篇文章主要介绍了如何在Python中输出\u编码并将其转换成中文,天达云小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随天达云小编来看看吧!
Python主要用来做什么
Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬虫;4、嵌入式应用开发;5、游戏开发;6、桌面应用开发。
# -*- 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页的信息

小结:
压注意的是
创建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编码并将其转换成中文,如何觉得天达云网站的内容还不错,欢迎将天达云网站推荐给身边好友。