成都鸿邑网站建设,长春企业网站如何建设,扫黄打非网站建设,东莞优化公司收费1、在python2和python3中的差异 在python2中#xff0c;urllib和urllib2各有各自的功能#xff0c;虽然urllib2是urllib的升级版#xff0c;但是urllib2还是不能完全替代urllib#xff0c;但是在python3中#xff0c;全部封装成一个类#xff0c;即urllib python2中urlli…1、在python2和python3中的差异 在python2中urllib和urllib2各有各自的功能虽然urllib2是urllib的升级版但是urllib2还是不能完全替代urllib但是在python3中全部封装成一个类即urllib python2中urllib2和urllib的区别 Urllib2可以接受一个Request对象并以此可以来设置一个URL的headers但是urllib只接受一个URL。这就意味着你不能通过urllib伪装自己的请求头。Urllib模板可以提供运行urlencode的方法该方法用于GET查询字符串的生成urllib2的不具备这样的功能而且urllib.quote等一系列quote和unquote功能没有被加入urllib2中因此有时也需要urllib的辅助。这就是urllib和urllib2一起使用的原因quote用来url转码的 import urllib.requesturllib.request.Request(url, dataNone, headers {}, method None)headers {User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3, Referer: http://www.lagou.com/zhaopin/Python/?labelWordslabel, Connection: keep-alive }http的头信息可以直接使用字典的形式 Request如果要发送data并无法直接传入字典类型的参数需要进行数据转换你可以直接使用类似于get传出参数的方法也可以使用urllib给我们提供的类 from urllib import request, parsedata {first: true, pn: 1, kd: Python}data parse.urlencode(data).encode(utf-8)print(data)#结果bfirsttruepn1kdPython‘urllib.parse.urlencode(query, doseqFalse, safe, encodingNone, errorsNone)urlencode主要作用就是将url附上要提交的数据。Post的数据必须是bytes或者iterable of bytes不能是str因此需要进行encode编码 urllib.request.urlopen(url, dataNone, timeoutNone) url 需要打开的网站data psot提交的数据Timeout 网站访问的超时时间但是没法伪装我们的头信息 from urllib import requestreq request.Request(url, headersheaders, datadata)html request.urlopen(req).read() 2、urllib的下载 from urllib import requesturl http://inews.gtimg.com/newsapp_match/0/2711870562/0request.urlretrieve(url, 1.jpg)或者通过from urllib import requesturl http://inews.gtimg.com/newsapp_match/0/2711870562/0req request.Request(url)res request.urlopen(req)text res.read()with open(2.jpg, wb) as f: f.write(text) 3、urllib的代理 from urllib import request, parsedata {first: true, pn: 1, kd: Python }url http://2017.ip138.com/ic.aspproxy request.ProxyHandler({http: 112.95.61.146:8118}) # 设置proxyopener request.build_opener(proxy) # 挂载opener# opener request.build_opener() # 挂载openerrequest.install_opener(opener) # 安装openerdata parse.urlencode(data).encode(utf-8)page opener.open(url, data).read()print(type(page))print(page.decode(gbk))结果body stylemargin:0pxcenter您的IP是[112.95.61.146] 来自广东省深圳市 联通/center/body/html 4、urllib的cookie使用 如果已经知道cookie或者说你是通过抓包获取到的cookie直接放在header的信息中直接登陆就可以登陆京东网站的cookie信息和不登录京东的cookie信息是不一样的你可以登录京东以后抓取cookie的信息然后访问任何网站就可以了 import urllib.requesturl http://www.jd.comheader {user-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36, cookie: xxxxx}req urllib.request.Request(urlurl, headersheader)res urllib.request.urlopen(req)text res.read() 5、urllib的cookie相关的类 在python2中cookie的类叫做import cookielib在python3中cookie的类叫做import http.cookiejar6、opener的概念 当你获取一个URL你使用一个opener(一个urllib2 OpenerDirector的实例)。在前面我们都是使用的默认的opener也就是urlopenurlopen是一个特殊的opener可以理解成opener的一个特殊实例传入的参数仅仅是urldatatimeout如果我们需要用到Cookie只用这个opener是不能达到目的的所以我们需要创建更一般的opener来实现对Cookie的设置7、终端输出cookie对象 import urllib.requestimport http.cookiejarurl http://www.hao123.comreq urllib.request.Request(url)cookiejar http.cookiejar.CookieJar()handler urllib.request.HTTPCookieProcessor(cookiejar)opener urllib.request.build_opener(handler)r opener.open(req)print(cookiejar)输出CookieJar[Cookie BAIDUID93B415355E0704B2BC94B5D514468898:FG1 for .hao123.com/, Cookie hz0 for .www.hao123.com/, Cookie ft1 for www.hao123.com/, Cookie v_pgnormal for www.hao123.com/] 8、Cookie保存到文件中 import urllib.requestimport http.cookiejarurl http://www.hao123.comreq urllib.request.Request(url)cookieFileName cookie.txtcookiejar http.cookiejar.MozillaCookieJar(cookieFileName)#文件cookiehandler urllib.request.HTTPCookieProcessor(cookiejar)opener urllib.request.build_opener(handler)r opener.open(req)print(cookiejar)cookiejar.save()保存在了文件cookie.txt中MozillaCookieJar继承FileCookieJar()继承CookieJar 9、Cookie从文件中读取cookie信息并访问 import urllib.requestimport http.cookiejarcookie_filename cookie.txtcookie http.cookiejar.MozillaCookieJar(cookie_filename)cookie.load(cookie_filename, ignore_discardTrue, ignore_expiresTrue)print(cookie)url http://www.hao123.comreq urllib.request.Request(url)handler urllib.request.HTTPCookieProcessor(cookie)opener urllib.request.build_opener(handler) # 利用urllib2的build_opener方法创建一个openerresponse opener.open(req)print(response.read().decode(“utf-8”))#解决乱码的问题 转载于:https://www.cnblogs.com/Jweiqing/p/9189367.html