Python 爬虫入门3种方法
2017-08-31 15:27
169 查看
Python 2.0
Python 3.0
参考:http://www.imooc.com/article/16363
url = "http://www.baidu.com" print '第一种方法' response1 = urllib2.urlopen(url) print response1.getcode() print len(response1.read()) print '第二种方法' request = urllib2.Request(url) request.add_header("user-agent","Mozilla/5.0") response2 = urllib2.urlopen(request) print response2.getcode() print len(response2.read()) print '第三种方法' cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) response3 = urllib2.urlopen(url) print response3.getcode() print cj print response3.read()
Python 3.0
第一种方法 import urllib.request import http.cookiejar url="http://www.baidu.com" print('第一种方法:') response1 = urllib.request.urlopen(url) print(response1.getcode()) print(len(response1.read())) print('第二种方法') request = urllib.request.Request(url) request.add_header('user-agent','Mozilla/5.0') response2 =urllib.request.urlopen(request) print(response1.getcode()) print(len(response2.read())) print('第三种方法') cj = http.cookiejar.CookieJar() opener= urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) urllib.request.install_opener(opener) response3 =urllib.request.urlopen(url) print(response3.getcode()) print(cj) print(response3.read())
参考:http://www.imooc.com/article/16363
相关文章推荐
- Python入门简单的静态网页爬虫2.0 (实现各模块的具体方法)
- 一种基于迭代与分类识别方法的入门级Python爬虫
- python技巧31[Python 动态加载模块的3种方法]
- Python一天入门15:面向对象的编程2-类与对象的方法
- Python入门网络爬虫之精华版
- Python爬虫入门(3):Urllib库的基本使用
- Python爬虫入门之request函数定制
- Python爬虫(入门+进阶)学习笔记 1-4 使用Xpath解析豆瓣短评
- Python爬虫入门-python之爬取pexels高清图片
- python爬虫get和post方法的使用以及cookie
- python3写爬虫程序时,遇到的问题及解决方法
- python爬虫框架Scrapy入门:安装
- Python爬虫教程——入门四之Urllib库的高级用法
- python3种编辑方法
- python爬虫入门urllib库的使用
- python爬虫从入门到放弃(三)之 Urllib库的基本使用
- python爬虫入门(七)Scrapy框架之Spider类
- python3 爬虫-入门
- python3 [爬虫入门实战]爬虫之selenium 安装设置与初步使用
- Python入门_浅谈字符串的分片与索引、字符串的方法