python爬虫01:urllib初体验
来源:互联网 发布:面向对象编程步骤 编辑:程序博客网 时间:2024/06/04 20:05
选择爬取网站为 http://www.qiushibaike.com/
翻页之后我们可以看到网址变化为http://www.qiushibaike.com/8hr/page/2/?s=4908225
也就是说,其page的变化决定了我们要爬取第几页糗百
因此url为
url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %pagepage可以自己定,固定的数字,乃至for循环爬取多页,此次初体验先爬取第一页
page = 1url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %page爬虫采用urllib,urllib2库来帮助获取回应的网页
于是,代码初步就出来了
import urllibimport urllib2page = 1url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %pagerequest = urllib2.Request(url)response = urllib2.urlopen(request)print response.read()但是 发现出现Traceback (most recent call last)错误,此错误常见有网页拒绝非人工(比如爬虫)访问、空间不够等。此处是因为网站检测到非法访问,拒绝了。
所以设置headers
user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"headers = { 'User-Agent' : user_agent }此处需要了解的是:
request(url,data,headers),data,heardes的默认参数以及什么时候需要使用
获取headers,user-agent,乃至cookies,proxy的方法(网页F12)
user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"headers = { 'User-Agent' : user_agent }request = urllib2.Request(url,headers=headers)因此代码变成了如下模样
import urllibimport urllib2page = 1url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %pageprint urluser_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"headers = { 'User-Agent' : user_agent }request = urllib2.Request(url,headers=headers)response = urllib2.urlopen(request)result=response.read()print result
运行成功
但是与原网页的源代码对比,发现 中文字符出现乱码(LINUX下正常显示)
是因为Python的编码问题,初步想法是通过正确的解码方式解决乱码问题,通过F12看到是utf-8编码,因此
response.read().decode("utf-8")
import urllibimport urllib2page = 1url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %pageprint urluser_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"headers = { 'User-Agent' : user_agent }request = urllib2.Request(url,headers=headers)response = urllib2.urlopen(request)result=response.read().decode("utf-8")print result
但是出现UnicodeEncodeError: 'gbk' codec can't encode character u'\ufeff' in position 42377: illegal multibyte sequence
感觉很奇怪,页面本身是UTF-8的,然后想要UTF-8解码,结果却提示GBK错误,之后走了一些弯路,暂且不表。
通过查找资料,得知对于此Unicode的字符,需要print出来的话,由于本地系统是Win7中的cmd,默认codepage是CP936,即GBK的编码,所以需要先将上述的Unicode的titleUni先编码为GBK,然后再在cmd中显示出来,然后由于titleUni中包含一些GBK中无法显示的字符,导致此时提示“’gbk’ codec can’t encode”的错误的。
得到解决方案
import urllibimport urllib2page = 1url = 'http://www.qiushibaike.com/8hr/page/%d/?s=4908218' %pageprint urluser_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"headers = { 'User-Agent' : user_agent }request = urllib2.Request(url,headers=headers)response = urllib2.urlopen(request)result=response.read().decode("utf-8").encode("GB18030")print result目前为止得到的request跟F12获得的页面内容相同。(LINUX下此方法反而乱码,多此一举)
- python爬虫01:urllib初体验
- python爬虫--urllib
- Python爬虫-urllib库
- [爬虫] Python爬虫 urllib BeautifulSoup
- Python3.6 爬虫初体验--urllib、beautifulsoup(一)
- python爬虫urllib使用B
- Python爬虫urllib笔记(一)
- Python爬虫之urllib介绍
- Python爬虫---urllib库介绍
- python爬虫-urllib库学习
- python爬虫(urllib简介)
- python 爬虫好文 urllib cookie beautifulsoap
- python爬虫入门-urllib的基本用法
- python爬虫中的 urllib 模块 浅析
- python爬虫-urllib+cookie+json+POST
- python爬虫--urllib2和urllib区别
- Python爬虫入门_之urllib2&&urllib
- python多线程爬虫学习--urllib的使用
- 图的储存(边集数组)
- Java 数组
- Android热补丁技术,ClassLoader、dexposed、Andfix、smart app updates补丁技术收集整理
- Android 6.0运行时权限
- 准确率召回率
- python爬虫01:urllib初体验
- 片段漫反射.Shader
- js中setInterval setTimeout的用法
- 给Java程序猿们推荐一些值得一看的好书
- OkHttp与Cookie及Cookie的持久化
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest A
- Android Studio项目与Eclipse+ADT 项目之间的相互转换
- InstallShield 用法
- YII2 隐藏index