Python学习--23 第三方库
本文将介绍python里常用的模块。如未特殊说明,所有示例均以python3.4为例:
$ python -V
Python 3.4.3
网络请求
urllib
urllib提供了一系列用于操作URL的功能。通过urllib我们可以很方便的抓取网页内容。
抓取网页内容
# coding: utf-8
import urllib.request
url = 'https://api.douban.com/v2/book/2129650'
with urllib.request.urlopen(url) as f:
    headers = f.getheaders() # 报文头部
    body = f.read() # 报文内容
    print(f.status, f.reason) # 打印状态码、原因语句
    for k,v in headers:
        print(k + ': ' + v)
    print(body.decode('utf-8'))
抓取百度搜索图片
import urllib.request
import os
import re
import time
url=r'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1488722322213_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%A3%81%E7%BA%B8%E5%B0%8F%E6%B8%85%E6%96%B0&f=3&oq=bizhi%E5%B0%8F%E6%B8%85%E6%96%B0&rsp=0'
imgPath=r'E:\img'
if not os.path.isdir(imgPath):
    os.mkdir(imgPath)
imgHtml=urllib.request.urlopen(url).read().decode('utf-8')
#test html
#print(imgHtml)
urls=re.findall(r'"objURL":"(.*?)"',imgHtml)
index=1
for url in urls:
    print("下载:",url)
    #未能正确获得网页 就进行异常处理
    try:
        res=urllib.request.urlopen(url)
        if str(res.status)!='200':
            print('未下载成功:',url)
            continue
    except Exception as e:
        print('未下载成功:',url)
    filename=os.path.join(imgPath,str(time.time()) + '_' + str(index)+'.jpg')
    with open(filename,'wb') as f:
        f.write(res.read())
        print('下载完成\n')
        index+=1
print("下载结束,一共下载了 %s 张图片"% (index-1))
python2.7的用户需要把urllib.request替换成urllib。
批量下载图片
# coding: utf-8
import os,urllib.request
url_path = 'http://www.ruanyifeng.com/images_pub/'
imgPath=r'E:\img'
if not os.path.isdir(imgPath):
    os.mkdir(imgPath)
index=1
for i in range(1,355):
    url = url_path + 'pub_' + str(i) + '.jpg'
    print("下载:",url)
    try:
        res = urllib.request.urlopen(url)
        if(str(res.status) != '200'):
            print("下载失败:", url)
            continue
    except:
        print('未下载成功:',url)
    filename=os.path.join(imgPath,str(i)+'.jpg')
    with open(filename,'wb') as f:
        f.write(res.read())
        print('下载完成\n')
        index+=1
print("下载结束,一共下载了 %s 张图片"% (index-1))
模拟GET请求附带头信息
urllib.request.Request实例化后有个add_header()方法可以添加头信息。
# coding: utf-8
import urllib.request
url = 'http://www.douban.com/'
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
with urllib.request.urlopen(req) as f:
    headers = f.getheaders()
    body = f.read()
    print(f.status, f.reason)
    for k,v in headers:
        print(k + ': ' + v)
    print(body.decode('utf-8'))
这样会返回适合iPhone的移动版网页。
发送POST请求
urllib.request.urlopen()第二个参数可以传入需要post的数据。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from urllib import request
from urllib.parse import urlencode
#----------------------------------
# 手机号码归属地调用示例代码 - 聚合数据
# 在线接口文档:http://www.juhe.cn/docs/11
#----------------------------------
def main():
    #配置您申请的APPKey
    appkey = "*********************"
    #1.手机归属地查询
    request1(appkey,"GET")
#手机归属地查询
def request1(appkey, m="GET"):
    url = "http://apis.juhe.cn/mobile/get"
    params = {
        "phone" : "", #需要查询的手机号码或手机号码前7位
        "key" : appkey, #应用APPKEY(应用详细页查询)
        "dtype" : "", #返回数据的格式,xml或json,默认json
    }
    params = urlencode(params).encode('utf-8')
    if m =="GET":
        f = request.urlopen("%s?%s" % (url, params))
    else:
        f = request.urlopen(url, params)
    content = f.read()
    res = json.loads(content.decode('utf-8'))
    if res:
        error_code = res["error_code"]
        if error_code == 0:
            #成功请求
            print(res["result"])
        else:
            print("%s:%s" % (res["error_code"],res["reason"]) )
    else:
        print("request api error")
if __name__ == '__main__':
    main()
Requests
虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好。它已经不适合现在的时代,不适合现代的互联网了。而Requests的诞生让我们有了更好的选择。
正像它的名称所说的,HTTP for Humans,给人类使用的HTTP库!在Python的世界中,一切都应该简单。Requests使用的是urllib3,拥有了它的所有特性,Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。现代、国际化、人性化。
官网:http://python-requests.org/
文档:http://cn.python-requests.org/zh_CN/latest/
Github主页:https://github.com/kennethreitz/requests
需要先安装:
$ pip3 install requests
Collecting requests
  Downloading requests-2.13.0-py2.py3-none-any.whl (584kB)
    100% |████████████████████████████████| 593kB 455kB/s
Installing collected packages: requests
Successfully installed requests-2.13.0
请求示例
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
url = 'https://api.github.com/user'
# r = requests.request('get', url, auth=('52fhy', ''))
r = requests.get(url, auth=('', ''))
print('Status: %s' % r.status_code) # 状态码
# 头信息
for k,v in r.headers.items():
    print(k + ': ' + v)
print('encoding: ' , r.encoding)
print('body: ' , r.text)
print('json body: ' , r.json())
默认情况下,dict迭代的是key。如果要迭代value,可以用
for value in d.values(),如果要同时迭代key和value,可以用for k, v in d.items()。
POST请求
基于表单的:
# coding: utf-8
import requests
payload = {'name': 'python', 'age': '11'}
r = requests.post("http://httpbin.org/post", data=payload)
print(r.text)
基于text的:
# coding: utf-8
import requests,json
payload = {'name': 'python', 'age': '11'}
r = requests.post("https://api.github.com/some/endpoint", data=json.dumps(payload))
print(r.text)
还可以使用 json 参数直接传递,然后它就会被自动编码。这是 2.4.2 版的新加功能:
r = requests.post("https://api.github.com/some/endpoint", json=payload)
hashlib
md5
import hashlib
md5 = hashlib.md5()
md5.update('how to use md5 in python hashlib?'.encode('utf-8'))
print(md5.hexdigest())
结果如下:
d26a53750bc40b38b65a520292f69306
update(),用于将内容分块进行处理,适用于大文件的情况。示例:
import hashlib
def get_file_md5(f):
    m = hashlib.md5()
    while True:
        data = f.read(10240)
        if not data:
            break
        m.update(data)
    return m.hexdigest()
with open(YOUR_FILE, 'rb') as f:
    file_md5 = get_file_md5(f)
对于普通字符串的md5,可以封装成函数:
def md5(string):
    import hashlib
    return hashlib.md5(string.encode('utf-8')).hexdigest()
SHA1
import hashlib
sha1 = hashlib.sha1()
sha1.update('py'.encode('utf-8'))
sha1.update('thon'.encode('utf-8'))
print(sha1.hexdigest())
等效于:
hashlib.sha1('python'.encode('utf-8')).hexdigest()
SHA1的结果是160 bit字节,通常用一个40位的16进制字符串表示。
此外,hashlib还支持sha224, sha256, sha384, sha512。
base64
Base64是一种用64个字符来表示任意二进制数据的方法。Python内置的base64可以直接进行base64的编解码:
>>> import base64
>>> base64.b64encode(b'123')
b'MTIz'
>>> base64.b64decode(b'MTIz')
b'123'
由于标准的Base64编码后可能出现字符+和/,在URL中就不能直接作为参数,所以又有一种"url safe"的base64编码,其实就是把字符+和/分别变成-和_:
>>> base64.b64encode(b'i\xb7\x1d\xfb\xef\xff')
b'abcd++//'
>>> base64.urlsafe_b64encode(b'i\xb7\x1d\xfb\xef\xff')
b'abcd--__'
>>> base64.urlsafe_b64decode('abcd--__')
b'i\xb7\x1d\xfb\xef\xff'
时间日期
该部分在前面的笔记里已做详细介绍:http://www.cnblogs.com/52fhy/p/6372194.html。本节仅作简单回顾。
time
# coding:utf-8
import time
# 获取时间戳
timestamp = time.time()
print(timestamp)
# 格式时间
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
# 返回当地时间下的时间元组t
print(time.localtime())
# 将时间元组转换为时间戳
print(time.mktime(time.localtime()))
t = (2017, 2, 11, 15, 3, 38, 1, 48, 0)
print(time.mktime(t))
# 字符串转时间元组:注意时间字符串与格式化字符串位置一一对应
print(time.strptime('2017 02 11', '%Y %m %d'))
# 睡眠
print('sleeping...')
time.sleep(2) # 睡眠2s
print('sleeping end.')
输出:
1486797515.78742
2017-02-11 15:18:35
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=11, tm_hour=15, tm_min=18, tm_sec=35, tm_wday=5, tm_yday=42, tm_isdst=0)
1486797515.0
1486796618.0
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=42, tm_isdst=-1)
sleeping...
sleeping end.
datetime
方法概览:
datetime.now() # 当前时间,datetime类型
datetime.timestamp() # 时间戳,浮点类型
datetime.strftime('%Y-%m-%d %H:%M:%S') # 格式化日期对象datetime,字符串类型
datetime.strptime('2017-2-6 23:22:13', '%Y-%m-%d %H:%M:%S') # 字符串转日期对象
datetime.fromtimestamp(ts) # 获取本地时间,datetime类型
datetime.utcfromtimestamp(ts) # 获取UTC时间,datetime类型
示例:
# coding: utf-8
from datetime import datetime
import time
now = datetime.now()
print(now)
# datetime模块提供
print(now.timestamp())
输出:
2017-02-06 23:26:54.631582
1486394814.631582
小数位表示毫秒数。
图片处理
PIL
PIL(Python Imaging Library)已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。
安装:
$ pip install Pillow
Collecting Pillow
  Downloading Pillow-4.0.0-cp34-cp34m-win32.whl (1.2MB)
Successfully installed Pillow-4.0.0
图像缩放:
# coding: utf-8
from PIL import Image
im = Image.open('test.jpg')
print(im.format, im.size, im.mode)
im.thumbnail((200, 100))
im.save('thumb.jpg', 'JPEG')
模糊效果:
# coding: utf-8
from PIL import Image,ImageFilter
im = Image.open('test.jpg')
im2 = im.filter(ImageFilter.BLUR)
im2.save('blur.jpg', 'jpeg')
验证码:
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
# 随机字母:
def rndChar():
    return chr(random.randint(65, 90))
# 随机颜色1:
def rndColor():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机颜色2:
def rndColor2():
    return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
# 240 x 60:
width = 60 * 4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
font = ImageFont.truetype('Arial.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill=rndColor())
# 输出文字:
for t in range(4):
    draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')
注意示例里的字体文件必须是绝对路径。
下载
you-get
安卓you-get
pin install you-get
需要有ffmpeg的支持。windows下载地址:
https://ffmpeg.zeranoe.com/builds/win64/static/ffmpeg-3.4.1-win64-static.zip
解压后添加环境变量,例如:
C:\Program Files\ffmpeg-3.4.1-win64-static\bin
然后就可以下载各大视频网站的视频了。示例:
you-get v.youku.com/v_show/id_XNTA5MDQ1OTM2.html
如果提示201:客户端未授权,可以安装Adobe Flash Player 28 PPAPI试试。
如果你在使用Mac,可以下载客户端:
https://github.com/coslyk/moonplayer
参考:
1、Python资源
http://hao.jobbole.com/?catid=144
Python学习--23 第三方库的更多相关文章
- python学习(23)requests库爬取猫眼电影排行信息
		本文介绍如何结合前面讲解的基本知识,采用requests,正则表达式,cookies结合起来,做一次实战,抓取猫眼电影排名信息. 用requests写一个基本的爬虫 排行信息大致如下图 网址链接为ht ... 
- Python中使用第三方库xlrd来写入Excel文件示例
		Python中使用第三方库xlrd来写入Excel文件示例 这一篇文章就来介绍下,如何来写Excel,写Excel我们需要使用第三方库xlwt,和xlrd一样,xlrd表示read xls,xlwt表 ... 
- Python学习day45-数据库(总结)
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习day44-数据库(单表及多表查询)
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习day43-数据库(多表关系)
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习day42-数据库的基本操作(1)
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Python学习day41-数据库(1)
		figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ... 
- Hadoop streaming使用自定义python版本和第三方库
		在使用Hadoop的过程中,遇到了自带python版本比较老的问题. 下面以python3.7为例,演示如何在hadoop上使用自定义的python版本以及第三方库. 1.在https://www.p ... 
- (python pip安装第三方库超时问题(raise ReadTimeoutErrorself._pool, None, 'Read timed out.')
		(python pip安装第三方库超时问题(raise ReadTimeoutErrorself._pool, None, ‘Read timed out.’)pip工具安装百度经验链接: pip安装 ... 
随机推荐
- DEV组件LookupEdit,ComboBoxEdit绑定数据源
			LookupEdit可以绑定数据表(DataTable)或对象数据组(Object List)作为数据源,下拉窗体可自定显示栏位. 绑定数据源需要设置三个参数:DisplayMember ,Value ... 
- Angular  - - $location 和 $window
			$location $location服务解析浏览器地址中的url(基于window.location)并且使url在应用程序中可用.将地址栏中的网址的变化反映到$location服务和$locati ... 
- CSS控制之IE常见BUG及解决方案
			常见bug 解决方案 盒模型bug 使用严格doctype声明 双倍margin bug _display:inline; 不认识a:link 不加:link 3像素margin bug 规范浮动与清 ... 
- Eclipse中javascript文件 clg 变为console.log();
			Eclipse中javascript文件 clg 变为console.log(); window>preferance>JavaScript>Editor>Templates ... 
- spring mvc controller中获取request head内容
			spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ... 
- 计算数据库中30天以内,30-60天,60-90天,90天以外的数据的个数(用sql实现)
			30天以内:select count(*) from TB where datediff(day,字段名,getdate()) between 0 and 3030-60天:select count( ... 
- Boost.Asio技术文档
			Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ... 
- overflow:hidden 你所不知道的事
			overflow:hidden 你所不知道的事 overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解. ... 
- Nancy简单实战之NancyMusicStore(二):打造首页
			前言 继上一篇搭建好项目之后,我们在这一篇中将把我们NancyMusicStore的首页打造出来. 布局 开始首页之前,我们要先为我们的整个应用添加一个通用的布局页面,WebForm中母版页的概念. ... 
- 【js 编程艺术】小制作二
			首先是一个html文档 /* explanation.html */<!DOCTYPE html> <html> <head> <meta charset=& ... 
