#!/usr/bin/python
2 import urllib
3 import urllib2
4 import re
5 import os
6
7 dirs = ['js','img','pay','css']
8 urls = ['http://www.xxxxxx.net/' + x for x in dirs]
9
10 def parse(baseurl):
11 url_hand = urllib2.urlopen(baseurl)
12 url_cont = url_hand.read()
13 urllist = re.findall("<A HREF=\".*\">",url_cont)
14 files = []
15 dirs = []
16 cwd = os.getcwd()
17 for x in urllist:
18 xx = x.split("\"")[1]
19 if re.search(".*/$",xx):
20 dirs.append(xx)
21 nextpath = os.path.join(cwd, xx)
22 else:
23 files.append(xx)
24 dirs.remove(dirs[0])
25
26
27 for xfile in files:
28 xfileurl = "http://www.xxxxxx.net" + xfile
29 #todir = os.path.join(pardir, os.path.dirname(xfile))
30 todir = cwd + xfile
31 print todir
32 urllib.urlretrieve(xfileurl, todir)
33 for xdir in dirs:
34 todir = cwd + xdir
35 try:
36 os.mkdir(todir)
37 except OSError, e:
38 print "dir exist!!"
39 xdirurl = "http://www.xxxxxx.net" + xdir
40 print xdirurl
41 parse(xdirurl)
42
43
44 if __name__ == "__main__":
45 for url in urls:
46 parse(url)

知识点:

1.这个站点有autoindex,所以进入目录后自动列出里面的文件,将其爬出,分类,文件,和目录

对于文件,直接抓取。

对于目录,得到路径后对其调用函数递归抓取。

2.下载文件,可以使用urllib模块的urlretrieve

3.还可以使用urlopen->read->write to file

[python]爬站点的更多相关文章

  1. 利用python爬取城市公交站点

    利用python爬取城市公交站点 页面分析 https://guiyang.8684.cn/line1 爬虫 我们利用requests请求,利用BeautifulSoup来解析,获取我们的站点数据.得 ...

  2. 用Python爬E站本

    用Python爬E站本 一.前言 参考并改进自 OverJerry 大佬的 教你怎么用Python爬取E站的本子_OverJerry. 本文为技术学习记录,不提供访问无存在网站的任何方法,也不包含不和 ...

  3. 用Python爬取网易云音乐热评

    用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...

  4. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  5. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  6. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  7. python爬爬爬之单网页html页面爬取

    python爬爬爬之单网页html页面爬取 作者:vpoet mail:vpoet_sir@163.com 注:随意copy 不用告诉我 #coding:utf-8 import urllib2 Re ...

  8. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  9. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

随机推荐

  1. JSP页面中文乱码问题

    $.get()方法到服务器端中文乱码 在jsp页面使用encodeURI(“中文”),在服务器端进行解码 String name = req.getParameter("name" ...

  2. n! 阶乘

    其实1.2.3.4.6.7…都是可以不用考虑的,因此选择以5为迭代步数即可. 首先,这些数字都可以不用进行%5(对5取余数)运算,因此每次循环时可以直接将函数的count变量直接加1.其次,考虑25. ...

  3. 1 wait notify

    wait/notify: wait()使线程停止,notify使wait状态的线程继续执行. wait()是Object类的方法,该方法用来将线程置入“预执行队列”,并在wait()方法处停止执行,直 ...

  4. 莱布尼兹三角形(C++)

    [问题描述] 如下图所示的三角形,请编程输出图中排在第 n 行从左边数第 m 个位置上的数. [代码展示] # include<iostream># include<cstdio&g ...

  5. 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列

    头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...

  6. New Year and Domino:二维前缀和

    题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...

  7. CDH问题集

    1.在CM中添加主机报JDK错误 手动在机器上安装oracle-jdk1.7+update64.然后在CM中选择不安装oracle-jdk即可. 2.HostMoinitor无法与server联系 查 ...

  8. 测试下markdown!

    目录 目的 代码 目的 测试markdown 代码 void static void main(args String[]){ System.out.println("hollw" ...

  9. 一些容易记混的c++相关知识点

    一些容易记混的c++相关知识. 截图自:<王道程序员面试宝典>

  10. Check the string

    A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...