python常见问题的解决

来源:互联网 发布:php从入门到精通 编辑:程序博客网 时间:2024/05/22 17:39
1.怎样使python读入中文文件srcfile = r"F:/python书籍/Python实战::四周实现爬虫系统/课程资料/Plan-for-combating-master/week1/1_2/1_2answer_of_homework/1_2_homework_required/index.html"f = open(srcfile.decode('utf8').encode('gbk'))  #先做utf-8编码,然后  gbk解码for text in f.readlines():duru     print textf.close()2.网页下载的三种方法01from  urllib.request  import urlopen#直接请求response =urlopen("https://www.baidu.com/")#获取状态码    200表示网页是连通的print(response.getcode())#读取内容print(response.read().decode("utf-8"))02from urllib.request import  Requestfrom urllib.request import urlopenfrom urllib import parse#创建request对象req=Request("http://zhidao.baidu.com/question/682148683071066052.html")post_Data=parse.urlencode([    ("",""),    ("",""),    ("",""),    ("","")])req.add_header("","")resp=urlopen(req,data=post_Data)03#----------------python 中图像库 image库的用法1.调整一张图片的灰度from PIL  import  Imageimport os#打开图像得到一个PIL图像对素img =  Image.open('np.jpg')#将其转换为一张灰度图img = img.convert('L')#存储该图片try :    img.save("test.png")except   IOError:    print("cannot convert")2.PIL生成缩略图     thumbnail (adj  极小的)from  PIL  import  Imageimport osimg = Image.open("np.jpg")img.thumbnail((55,55))  #缩放为一个128*128的图像#存储图像try :    img.save("test.jpg")except:    print("cannot convert!")3.PIL调整尺寸与旋转from PIL  import  Imageimport osimg = Image.open("np.jpg")#Modify  image size ,parameters  for  a tupeimg = img.resize((100,100))#使图片逆时针旋转45img = img.rotate(45)#存储该图片try:    img.save("test.png")except:    print("cannot save!!")(未完待续。。。。。。。)
0 0
原创粉丝点击