利用正则表达式下载百度贴吧图片。百度贴吧直接用 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)
有更好的写法,望大家提出宝贵的建议供大家一起学习交流。
来源: http://www.bubuko.com/infodetail-1954896.html