- #!/usr/bin/python
- #encoding='utf-8'
- #runing under python 3.x
- import urllib.request
- from bs4 import BeautifulSoup
- import os
- def download(blocknum, blocksize, totalsize):
- '''回调函数
- @blocknum: 已经下载的数据块
- @blocksize: 数据块的大小
- @totalsize: 远程文件的大小
- '''
- percent = 100.0 * blocknum * blocksize / totalsize
- if percent > 100:
- percent = 100
- print("{0:3.2f}%%".format(percent))
- timeout = 60
- url = "http://"
- socket = urllib.request.urlopen('http://newbbs.fengniao.com/showthread.php?p=23712356')
- data = socket.read()
- socket.close()
- html = data.decode("utf-8")
- soup = BeautifulSoup(data)
- infoTags = soup.select('.mainContent')
- for contentTag in infoTags:
- imgTag = contentTag.find('img')
- if imgTag == None:
- break
- fileURL = imgTag['src']
- pos = fileURL.rfind('/')
- fileName = fileURL[(pos+1):]
- localPath = "d:/pic/"
- if not os.path.isdir(localPath):
- os.makedirs(localPath)
- localPath += fileName
- print(localPath)
- urllib.request.urlretrieve(fileURL, localPath, download)
- #该片段来自于http://www.codesnippet.cn/detail/210520149615.html
来源: http://www.codesnippet.cn/detail/210520149615.html