python爬虫基础知识(一)--Urllib.request
2017-12-11 12:30
721 查看
explain:The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.
1.urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
打开网址url,可以是一个字符串或者Request对象。
比如:
2.urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
Copy a network object denoted by a URL to a local file. If the URL points to a local file, the object will not be copied unless filename is supplied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object). Exceptions are the same as for urlopen().
这个函数是打来一个url并且保存到本地文件,返回的是一个tuple对象,同时这个函数在执行的过程中,还会有一定的缓冲,用urllib.request.urlcleanup()这个函数就可以清除缓存。
3.模拟浏览器修改报头
方法1:
使用build_opener()进行。
方法2:
使用add_header()。
4.urllib.parse.quote(string, safe=’/’, encoding=None, errors=None)
解决url编码问题,默认为utf-8,safe表示可以忽略的字符,error默认为replace,meaning invalid sequences are replaced by a placeholder character.
5.get请求步骤
构建对于的url–>以对应的URL为参数,构建Request对象–>
通过urlopen()打开构建的Request对象–>后续操作
6.post请求步骤
1)设置URl网址
2)构建表单数据,使用urllib.parse.urlkencode进行编码处理
3)创建Request对象
4)add_header()添加头信息
5)使用urllib.request.urlopen()打开提交并处理
7.代理服务器设置
1.urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)
打开网址url,可以是一个字符串或者Request对象。
比如:
file=urllib.request.urlopen("http://www.baidu.com") print(file.read())#读取所有内容赋给一个字符串 print(file.readline())#一行 print(file.readlines())#读取所有内容赋给一个列表 file.info()#获取当前环境有关的信息 file.getcode()#获取返回的状态码 file.geturl()#获取源网页地址
2.urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
Copy a network object denoted by a URL to a local file. If the URL points to a local file, the object will not be copied unless filename is supplied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object). Exceptions are the same as for urlopen().
这个函数是打来一个url并且保存到本地文件,返回的是一个tuple对象,同时这个函数在执行的过程中,还会有一定的缓冲,用urllib.request.urlcleanup()这个函数就可以清除缓存。
filname=urllib.request.urlretrieve("http://www.baidu.com",filename="data2.html") urllib.request.urlcleanup() print(filname)
3.模拟浏览器修改报头
方法1:
使用build_opener()进行。
import urllib.request opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] opener.open('http://www.example.com/')
方法2:
使用add_header()。
import urllib.request req = urllib.request.Request('http://www.example.com/') req.add_header('Referer', 'http://www.python.org/') # Customize the default User-Agent header value: req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') r = urllib.request.urlopen(req)
4.urllib.parse.quote(string, safe=’/’, encoding=None, errors=None)
解决url编码问题,默认为utf-8,safe表示可以忽略的字符,error默认为replace,meaning invalid sequences are replaced by a placeholder character.
key="粑粑" key_code=urllib.parse.quote(key) print(key_code) url="http://www.baidu.com/s?wd="+key_code
5.get请求步骤
构建对于的url–>以对应的URL为参数,构建Request对象–>
通过urlopen()打开构建的Request对象–>后续操作
import urllib.request,urllib.parse url="http://www.baidu.com/s?wd=" key="重庆" key_code=urllib.parse.quote(key) urlall=url+key_code req=urllib.request.Request(urlall) data=urllib.request.urlopen(req).read() print(data)
6.post请求步骤
1)设置URl网址
2)构建表单数据,使用urllib.parse.urlkencode进行编码处理
3)创建Request对象
4)add_header()添加头信息
5)使用urllib.request.urlopen()打开提交并处理
7.代理服务器设置
def use_proxy(proxy_address,url): import urllib.request proxy=urllib.request.ProxyHandler({'http':proxy_address}) opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandler) urllib.request.install_opener(opener)#全局默认的opener对象 data=urllib.request.urlopen(url).read().decode('utf-8') return data proxy_add="112.74.32.237:6666" data=use_proxy(proxy_add,"http://www.baidu.com") print(len(data))
相关文章推荐
- python 爬虫入门(2) 爬虫基础知识 ; urllib 模块 ;urllib2 模块
- Python基础知识——urllib模块在爬虫中的应用
- python 3.x 爬虫基础---Urllib详解
- Python的Urllib库的使用(爬虫基础)
- 在python3中使用urllib.request编写简单的网络爬虫
- Python 爬虫基础知识
- 零基础写python爬虫之urllib2使用指南
- Python 爬虫基础 - Urllib 模块(1)
- 零基础写python爬虫之urllib2使用指南
- python爬虫基础知识(二)--正则表达式
- 自学Python四 爬虫基础知识储备
- 零基础写python爬虫之urllib2中的两个重要概念:Openers和Handlers
- Python爬虫----基础知识(简单爬虫架构、URL管理器和实现方法)
- 【实战\聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎项目笔记】第3章 爬虫基础知识回顾
- python 入门爬虫 -基础知识(数据如何呈现【一】)
- python与matlab爬虫百度图片首页的图片(urllib.request)
- python爬虫基础之request设置问题
- Python基础知识——爬虫入门
- python爬虫之urllib.request和cookie登录CSDN
- Python零基础入门十九之爬虫基础知识