python3 urllib 爬虫乱码问题解决
来源:互联网 发布:怎样避免淘宝找同款 编辑:程序博客网 时间:2024/06/07 11:42
#!/usr/bin/env python# -*- coding: utf-8 -*-from bs4 import BeautifulSoupfrom urllib.request import urlopenbaseUrl = 'http://www.51bengou.com'def getCover(articleUrl): global baseUrl html = urlopen(baseUrl+articleUrl).read() bsObj = BeautifulSoup(html, 'lxml') try: # Find internal link of the cover. src = bsObj.find('div', {'class': 'cartoon-intro'}).find('img')['src'] return src except AttributeError: return Nonedef getInfo(articleUrl): global baseUrl html = urlopen(baseUrl+articleUrl) bsObj = BeautifulSoup(html, 'lxml') try: # Find all information. infos = bsObj.find('div', {'class': 'cartoon-intro'}).findAll('p', {'class': False}) items = {} # Store in a dict. for info in infos[:7]: items[info.span.text] = info.text return list(items) except AttributeError: return Noneprint(getInfo('/cartoon/HuoYingRenZhe/'))
如上程序是一个基于笨狗漫画网的爬虫程序,运行后,发现得到的漫画基本信息输出为乱码。
<meta charset="gb2312">
经查看网页源码发现,这个网页使用了gb2312编码。经我目前学习的编码知识,在程序读取网页时,BeautifulSoup使用了默认的utf-8编码将gb2312编码的字节字符串解码为了Unicode。此时,就出现了乱码,并且可能因为对错误的忽略或者替代,信息已经发生了丢失。
为了解决这个问题,我们应该在使用BeautifulSoup之前,对urlopen得到的对象进行读取,然后使用gb2312编码进行解码,此时问题应该就解决了。
#!/usr/bin/env python# -*- coding: utf-8 -*-from bs4 import BeautifulSoupfrom urllib.request import urlopenbaseUrl = 'http://www.51bengou.com'def getCover(articleUrl): global baseUrl html = urlopen(baseUrl+articleUrl).read().decode('gb2312', 'replace') bsObj = BeautifulSoup(html, 'lxml') try: # Find internal link of the cover. src = bsObj.find('div', {'class': 'cartoon-intro'}).find('img')['src'] return src except AttributeError: return Nonedef getInfo(articleUrl): global baseUrl html = urlopen(baseUrl+articleUrl).read().decode('gb2312', 'replace') bsObj = BeautifulSoup(html, 'lxml') print(html) try: # Find all information. infos = bsObj.find('div', {'class': 'cartoon-intro'}).findAll('p', {'class': False}) items = {} # Store in a dict. for info in infos[:7]: items[info.span.text] = info.text return list(items) except AttributeError: return Noneprint(getInfo('/cartoon/HuoYingRenZhe/'))
阅读全文
1 0
- python3 urllib 爬虫乱码问题解决
- Python3 urllib库爬虫 基础
- python3 urllib爬虫抓取记录
- Python3网络爬虫(三):urllib.error异常
- python3使用urllib模块制作网络爬虫
- Python3.5爬虫urllib系列之三
- python3+urllib撸新浪滚动新闻爬虫
- Python3 爬虫--公司代理问题解决
- python3.4爬虫——urllib等模块的引用
- urllib模块爬虫在Python2在Python3中的使用
- python3网络爬虫一《使用urllib.request发送请求》
- Python3.6 爬虫初体验--urllib、beautifulsoup(一)
- python3爬虫初探(一)之urllib.request
- python3爬虫攻略(1):urllib.request使用(1)
- python3爬虫攻略(2):urllib.request(2)
- Python3爬虫学习笔记(1.urllib库详解)
- python3网络编程乱码问题解决
- python3爬虫无法显示中文问题解决
- python+selenium自动化测试之路Day3之pip使用错误详解
- HTML笔记2
- 程序员210行纯css代码制作日出动画特效
- bzoj1602 [Usaco2008 Oct]牧场行走
- 源码安装 lnmp
- python3 urllib 爬虫乱码问题解决
- kendoui中的TreeView学习使用
- cmake+vs2017+opencv3.3.0编译contrib模块详解
- 使用RecyclerView结合jiaozivideoplayer去加载不同类型的布局
- windows安装jdk
- 篡改Java final参数
- hadoop 命令执行报错解决
- Codeforces Round #444(Div.2) B. Cubes for Masha
- Spark Streaming开发入门——WordCount(Java&Scala)