网页爬虫 - python3爬链家utf8页面,中文全部是“南京小区二手房(南京链家网)”
迷茫
迷茫 2017-04-18 09:58:42
0
2
1824
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

Antworte allen(2)
阿神
# -*- coding: utf-8 -*-


import requests
from bs4 import BeautifulSoup

url = 'http://nj.lianjia.com/xiaoqu/'
html = requests.get(url)
soup = BeautifulSoup(html.text.encode(html.encoding), 'lxml', from_encoding='utf-8')
title = soup.title.get_text()
print(title)

1) 加 # -- coding: utf-8 --声明
2) 正确的处理返回的response的编码

小葫芦

楼上没错,实际上你也可以用Latin1编码来解码这段文字。

import requests
from bs4 import BeautifulSoup

url = 'http://nj.lianjia.com/xiaoqu/'
html = requests.get(url)
soup = BeautifulSoup(html.text, 'lxml')
title = soup.title.get_text()
print(title.encode('latin1').decode('utf-8'))
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage