python 小程序

1、
#-*- coding:utf-8 -*- 
import urllib.request #导入模块
page =urllib.request.urlopen('http://tieba.baidu.com/p/1753935195')
htmlcode=page.read()

最开始程序如下

 #coding:utf-8
 import urllib
 page = urllib.urlopen('http://tieba.baidu.com/p/1753935195')#打开网页
 htmlcode = page.read()#读取页面源码

运行时报错:AttributeError: module 'urllib' has no attribute 'urlopen'

百度了下才知道Python3.X中应该用urllib.request。更改后就不会再出现这个错误了。

pageFile=open('pageCode.txt','wb')
pageFile.write(htmlcode)
pageFile.close()

此处又遇到问题,最开始程序如下

 pageFile = open('pageCode.txt','w')#以写的方式打开pageCode.txt
 pageFile.write(htmlcode)#写入
 pageFile.close()

报错:TypeError: write() argument must be str, not bytes

继续度娘发现,原来是文件打开方式有问题,把之前的打开语句修改为用二进制方式打开就没有问题,也就是‘wb+’

open()函数,传入标识符'w'或者'wb'表示写文本文件或写二进制文件

综上:

#-*- coding:utf-8 -*-
import urllib.request
page =urllib.request.urlopen('http://tieba.baidu.com/p/1753935195')
htmlcode=page.read()

pageFile=open('pageCode.txt','wb')
pageFile.write(htmlcode)
pageFile.close()

此处,已将网页内容存储到文件pageCode.txt中。

2、

#获取页面
def get_html(url):
    page=urllib.request.urlopen(url)
    html=page.read()
    html=html.decode('utf-8')#python3必须加上,不然会报错,错误如(1)所示
    return html

reg = r'src="(.+?\.jpg)" width'#正则表达式
reg_img = re.compile(reg)#编译一下,运行更快
imglist = reg_img.findall(get_html('http://tieba.baidu.com/p/1753935195'))
x = 0
for img in imglist:
    # print (img)
    urllib.urlretrieve(img,'%s.jpg'%x) #报错(2)
    urllib.request .urlretrieve(img,'%s.jpg'%x)
    x +=1

(1)TypeError: cannot use a string pattern on a bytes-like object  

解决办法:

加上html=html.decode('utf-8') #python3这句代码

(2)AttributeError: module 'urllib' has no attribute 'urlretrieve'

解决办法:

python3的urllib没有urlretrieve的,它在request 中的,所以你首先要用

import urllib.request

urllib.request .urlretrieve(img,'%s.jpg'%x)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值