python练习册 每天一个小程序 第0013题
生活随笔
收集整理的這篇文章主要介紹了
python练习册 每天一个小程序 第0013题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# -*-coding:utf-8-*—
'''題目描述:用 Python 寫一個爬圖片的程序,爬 這個鏈接里的日本妹子圖片 :-)地址:http://tieba.baidu.com/p/2166231880思路:用正則表達式匹配圖片鏈接,然后進行下載'''
'''
import re
import requestsdef main():url = 'http://tieba.baidu.com/p/2166231880'response = requests.get(url)html = response.textmatch = re.compile('img .*?src=\"(.*?)\"')for i in match.findall(html):if 'imgsrc' in i :print iif __name__ == '__main__':main()
'''import urllib2
import re
from os.path import basename
from urlparse import urlspliturl = "http://tieba.baidu.com/p/2166231880"
def getPage(url):url=url+"?see_lz=1"urlContent = urllib2.urlopen(url).read()page='<span class="red">(.*?)</span>'thePage=re.findall(page,urlContent)return int(thePage[0])
def downImg(url):urlContent = urllib2.urlopen(url).read() spans='<cc>(.*?)</cc>'ss=re.findall(spans,urlContent)obImgs=','.join(ss)imgUrls = re.findall('img .*?src="(.*?)"', obImgs)for imgUrl in imgUrls:print imgUrl'''try:imgData = urllib2.urlopen(imgUrl).read()fileName = basename(urlsplit(imgUrl)[2])output = open(fileName,'wb')output.write(imgData)output.close()except:print "Er.."'''
def downLoad(url):numb=getPage(url)cont=0print "There are "+str(numb)+" pages."while cont<numb:cont+=1print "Downloading "+url+"?see_lz=1&pn="+str(cont)+"..."downImg(url+"?see_lz=1&pn="+str(cont))print 'Completed!'downImg(url)
?
轉載于:https://www.cnblogs.com/deen-/p/7205105.html
總結
以上是生活随笔為你收集整理的python练习册 每天一个小程序 第0013题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ACCP8.0Y2Web前端框架与移动应
- 下一篇: luogu P1027 Car的旅行路