python爬虫下载网站所有文件
2017-08-19 16:58
791 查看
核心思路是使用正则表达式对网页的html5中的路径名和文件名进行抓取,
然后对路径继续进行同样的抓取,用递归的方式进行搜索。最后把网站上的内容文件全部下载下来
import urllib
import sys
import BeautifulSoup
import re
import os
path = []
def extract(url):
content = urllib.urlopen(url).read()
#reg = r'(?:href|HREF)="?((?:http://)?.+?\.txt)'
reg = r'<a href="(.*)">.*'
url_re = re.compile(reg)
url_lst = re.findall(url_re, content)
for lst in url_lst:
ext = lst.split('.')[-1]
if ext[-1] == '/':
newUrl = url + lst
extract(newUrl)
else:
path.append(url + lst)
print "downloading with urllib"
url = 'http://139.196.233.65/js/'
extract(url)
filePath = 'E:/6-学习文档/91-JS/Download/js'
filePath = unicode(filePath, 'utf8')
for p in path:
fileTitle = p.split('/js')[-1]
file = filePath + fileTitle
dir = os.path.dirname(file)
isExists=os.path.exists(dir)
if isExists == False:
os.makedirs(dir)
urllib.urlretrieve(p, file)
#for lst in url_lst:
# file = filePath + lst
# lst = url + '/' + lst
# urllib.urlretrieve(lst, file)
然后对路径继续进行同样的抓取,用递归的方式进行搜索。最后把网站上的内容文件全部下载下来
import urllib
import sys
import BeautifulSoup
import re
import os
path = []
def extract(url):
content = urllib.urlopen(url).read()
#reg = r'(?:href|HREF)="?((?:http://)?.+?\.txt)'
reg = r'<a href="(.*)">.*'
url_re = re.compile(reg)
url_lst = re.findall(url_re, content)
for lst in url_lst:
ext = lst.split('.')[-1]
if ext[-1] == '/':
newUrl = url + lst
extract(newUrl)
else:
path.append(url + lst)
print "downloading with urllib"
url = 'http://139.196.233.65/js/'
extract(url)
filePath = 'E:/6-学习文档/91-JS/Download/js'
filePath = unicode(filePath, 'utf8')
for p in path:
fileTitle = p.split('/js')[-1]
file = filePath + fileTitle
dir = os.path.dirname(file)
isExists=os.path.exists(dir)
if isExists == False:
os.makedirs(dir)
urllib.urlretrieve(p, file)
#for lst in url_lst:
# file = filePath + lst
# lst = url + '/' + lst
# urllib.urlretrieve(lst, file)
相关文章推荐
- Python爬虫小实践:下载妹子图www.mzitu.com网站上所有的妹子图片,并按相册名字建立文件夹分好文件名
- python学习(二)爬虫——爬取网站小说并保存为txt文件(二)
- Python爬虫实战(四) :下载煎蛋网所有妹子照片
- 【zz】Python2 批量下载网站文件
- python学习(二)爬虫——爬取网站小说并保存为txt文件(一)
- Python爬虫之一:十几行代码下载王者荣耀所有皮肤
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
- Python 网络爬虫 004 (编程) 如何编写一个网络爬虫,来下载(或叫:爬取)一个站点里的所有网页
- Python爬虫学习笔记——自动爬取XX网站,下载种子,封面,番号
- Java爬虫实战(二):抓取一个视频网站上2015年所有电影的下载链接
- 用python下载xxxx网站封面作品的所有图片
- python爬虫(3.下载文件)
- python 爬虫学习<将某一页的所有图片下载下来>
- Python爬虫(02)从网站下载图片
- 使用python进行爬虫下载指定网站的图片
- Python爬虫下载网站上的图片到本地
- python 爬虫 网络小说下载(静态网站)
- python爬虫(Python读取TXT文件中的URL并下载文件)
- Python爬虫实战(五) :下载百度贴吧帖子里的所有图片
- 【python】爬虫2——下载亦舒博客首页所有文章