利用正则表达式下载百度贴吧图片。百度贴吧直接用urllib.urlopen就可打开,像‘糗事百科’这种就打不开了,后面我呈上利用urllib2.Request来突破封禁抓取糗事百科的代码
#!/usr/bin。/python #coding=utf-8 import urllib import re def gethtml(url): page=urllib.urlopen(url) html=page.read() return html def getjpgurl(html): r=r‘src="(.*?\.jpg)" size‘ jpgurl=re.findall(r,html) print jpgurl x=1 for jpgurls in jpgurl: urllib.urlretrieve(jpgurls,‘%s.jpg‘%x) x+=1 html=gethtml(‘http://tieba.baidu.com/p/4968182754‘) getjpgurl(html)
有更好的写法,望大家提出宝贵的建议供大家一起学习交流。
本文出自 “全球互联云主机Q874247458” 博客,请务必保留此出处http://gosweet.blog.51cto.com/11759495/1900076
原文:http://gosweet.blog.51cto.com/11759495/1900076