Heim > Fragen und Antworten > Hauptteil
python爬取网络小说 中文‘乱码’,因为不知道是否是乱码,所以加了引号
代码如下
# -- coding:utf8 --
from bs4 import BeautifulSoup
import requests
url = "http://www.cishuge.com/read/0/250/"
web_data = requests.get(url)
soup = BeautifulSoup(web_data.text, 'lxml')
titles = soup.select('#readerlist > ul > li > a')
for title in titles:
data = {
'title': title.get('title'),
'link': title.get('href')
}
print(data)
目标网页为http://www.cishuge.com/read/0/250/
运行结果如下图所示
链接能正常显示,文章标题貌似‘乱码’
百度下没找到解决方法,特来求助各位前辈
补充:运行环境 windows10, python3, pycharm
巴扎黑2017-04-17 17:24:06
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
web_data.encoding = "gbk"
ringa_lee2017-04-17 17:24:06
1、看一下别人网站的编码
2、需要转码就转码
参考 https://github.com/bravekingzhang/moveSpider/blob/master/dyspider/html_downloader.py