python项目之爬虫实例(一)
2018-04-07 22:14
555 查看
1、使用urllib.request模块实现爬虫
import urllib.request url="http://www.2345.com" #请求该url,并返回页面数据 data=urllib.request.urlopen(url) #将data数据读取出来 html=data.read() print(html) #将网页放入一个文件 file=open("F:/1.html",'wb') file.write(html) file.close()
2.1、模拟浏览器访问url(使用opener对象的addhandlers属性
import urllib.request """模拟浏览器访问url的过程,对url进行访问 首先,创建opener对象(参数为handlers), 其次,使用opener对象的属性添加请求头(addheaders), 最后使用opener的open方法()/urllib.request的urlopen()方法请求页面数据 """ #页面的url url="http://www.2345.com" opener=urllib.request.build_opener() #添加opener的请求头(列表形式) header=("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36") opener.addheaders=[header] #使用opener的open()方法,访问url data=opener.open(url).read() #将data存入文件中 file=open("F:/2.html",'wb') file.write(data) file.close()
2.2、模拟浏览器访问url(使用Request对象的add_handler()方法)
import urllib.request """模拟浏览器访问url的过程,对url进行访问 首先,使用Request(url)方法创建一个Requet对象, 其次,使用Request对象的add_header()方法添加请求头 最后,使用urllib.request的urlopen(Request对象)方法访问 """ #url url="http://www.2345.com" #创建一个Request对象 req=urllib.request.Request(url) #创添加Request对象的add_header()方法(有两个参数) req.add_header("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36") #使用浏览器模拟访问url ht=urllib.request.urlopen(req) #将访问后的页面读取出来 data=ht.read() #将data存入文件中 file=open("F:/3.html",'wb') file.write(data) file.close()
3、使用get请求爬取页面
import urllib.request """使用get请求进行访问url 首先,构建对应的url(该url包含get请求的字段名和字段内容等信息,且url必须是get请求的形式) 其次,使用urllib.request.Request(url)创建一个Request对象, 最后使用urlopen()方法访问该url """ #提取出对应key的value值 value="古风" #对对应的value值进行网址编码 value1=urllib.request.quote(value) #构造可以改变的url url="https://www.sogou.com/sogou?pid=sogou-site-488e4104520c6aab&ie=utf8&query="+value1 #创建Reauest对象 req=urllib.request.Request(url) #访问对应的url ht=urllib.request.urlopen(req) #将页面读取到data data=ht.read() #将data存入文件中 file=open("F:/5.html",'wb') file.write(data) file.close()
4、使用post请求访问页面
import urllib.request import urllib.parse """使用post请求进行访问url(即点击提交按钮后,跳转到的页面就是处理post表单数据的url) 首先,设置对应的url 4000 (即post表单的数据会被提交并进行处理的url), 其次,构件post的表单数据(是一个字典对象)--查看网页源代码的表单的属性,并使用urllib.request.urlencode(), 然后,创建Request对象, 最后,使用urllib.request.urlopen(Request对象)方法访问一个url """ #处理表单数据的url url="http://www.iqianyue.com/mypost/" #将post表单的属性表示为字典对象,并进行编码 post={"name":"aas","pass":"123456"} post1=urllib.parse.urlencode(post).encode('utf-8') #创建一个Request对象 req=urllib.request.Request(url,post1) #访问url ht=urllib.request.urlopen(req) data=ht.read() #将data存入文件中 file=open("F:/6.html",'wb') file.write(data) file.close()
5、使用代理IP访问url
import urllib.request import urllib.parse """使用代理ip访问url 首先,创建handler 其次,使用urllib.request.build_opener(handlers)方法, 最后,使用urllib.request.urlopen(url)/opener的open(url)方法方法访问url """ #url url="http://www.2345.com" #创建ProxyHandler(代理handler) handler=urllib.request.ProxyHandler({'http':"代理IP地址"}) #创建opener对象 opener=urllib.request.build_opener(handler,urllib.request.HTTPHandler) #创建全局默认的opener对象 opener1=urllib.request.install_opener(opener) #访问url ht=urllib.request.urlopen(url) #将页面数据读取到data datat=ht.read() #将data存入文件中 file=open("F:/6.html",'wb') file.write(data) file.close()
6、使用cookie进行登录
import urllib.request import http.cookiejar import urllib.parse """使用cookie会话跟踪,进行访问url 首先,创建对应的Request对象(post请求) 首先,创建CookieJar对象 其次,创建cookie的handler 然后,创建opener对象,并创建全局的opener 最后,访问url """ #url url1="http://www.2345.com/post" #创建post post=urllib.parse.urlencode({post数据--字典}).encode() #创建Request对象 req=urllib.request.Request(url1,post) #创建CookieJar对象 cookiejar=http.cookiejar.CookieJar() #创建cookie的handler handler=urllib.request.HTTPCookieProcessor(cookiejar) #创建opener opener=urllib.request.build_opener(handler) urllib.request.install_opener(opener) #访问url1(登录页面) opener.open(req) #url2 url2="http://www.2345.com/post/index1" #访问登录页面下的另一个页面url2 urllib.request.urlopen(url2)
相关文章推荐
- python爬虫实例项目大全
- Python3 大型网络爬虫实战 002 --- scrapy 爬虫项目的创建及爬虫的创建 --- 实例:爬取百度标题和CSDN博客
- python爬虫实例项目大全
- python爬虫实例项目大全
- python爬虫实例项目大全
- python爬虫实例项目大全
- python爬虫实例项目大全
- python爬虫实例项目大全
- [项目实训]Python爬虫基本库回顾及实例练习
- Python爬虫实例:用requests重构豆瓣热播电影爬虫
- 初学python,爬虫小项目
- python爬虫从入门到放弃(九)之 实例爬取上海高级人民法院网开庭公告数据
- 16Python爬虫---Scrapy目录结构以及项目创建
- 【实战\聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎项目笔记】第3章 爬虫基础知识回顾
- opencv3视频中检测主播人脸 python 项目完整源代码实例
- Python 爬虫 简单实例 爬取糗事百科段子
- python爬虫实例——用scarpy框架爬取全部新浪新闻
- Python 爬虫实例(2)—— 爬取今日头条
- Python即时网络爬虫项目启动说明
- Python 爬虫实例(15) 爬取 汽车之家(汽车授权经销商)