import sys
import multiprocessing
import re
import os
import urllib.request as lib def craw_links( url,depth,keyword,processed):
''' url:the url to craw
deth:the current depth to craw
keyword:the tuple of keywords to focus
pool:process pool
''' contents=[]
if url.startswith(('htpp://','https://')):
if url not in processed:
#mark this url as processed
processed.append(url)
else:
#avoid prossing the same url again
return
print('Crawing '+url+'...')
fp = lib.urlopen(url)
#python3 returns bytes,so need to decode
contents = fp.read()
contents_decoded = contents.decode('UTF-8')
fp.close()
pattern = '|'.join(keyword)
#if this page contains certain keywords,save it to a file
flag = False
if pattern:
searched = re.search(pattern,contents_decoded)
else:
#if the keywords to filter is not given,save current page
flag = True
if flag or searched:
with open('craw\\'+url.replace(':','_').replace('/','_'),'wb') as fp:
fp.write(contents)
#find all the links in the current page
links = re.findall('href="(.*?)"',contents_decoded)
#craw all links in the current page
for link in links:
#consider the relative path
if not link.startswith(('http://','https://')):
try:
index=url.rindex('/')
link = url[0:index+1]+link
except:
pass
if depth>0 and link.endswith(('.htm','.html')):
craw_links(link,depth-1,keyword,processed) if __name__ == '__main__':
processed = []
keywords = ('KeyWord1','KeyWord2')
if os.path.exists('craw') or not os.path.isdir('craw'):
os.mkdir('craw')
craw_links(r'http://docs.python.org/3/library/index.html',1,keywords,processed)

Python_网页爬虫的更多相关文章

  1. cURL 学习笔记与总结(2)网页爬虫、天气预报

    例1.一个简单的 curl 获取百度 html 的爬虫程序(crawler): spider.php <?php /* 获取百度html的简单网页爬虫 */ $curl = curl_init( ...

  2. c#网页爬虫初探

    一个简单的网页爬虫例子! html代码: <head runat="server"> <title>c#爬网</title> </head ...

  3. 网页爬虫--scrapy入门

    本篇从实际出发,展示如何用网页爬虫.并介绍一个流行的爬虫框架~ 1. 网页爬虫的过程 所谓网页爬虫,就是模拟浏览器的行为访问网站,从而获得网页信息的程序.正因为是程序,所以获得网页的速度可以轻易超过单 ...

  4. 网页爬虫的设计与实现(Java版)

    网页爬虫的设计与实现(Java版)     最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...

  5. Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)

    原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...

  6. [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

    reference: http://www.52nlp.cn/python-%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab-%e6%96%87%e6%9c%ac%e5%a4% ...

  7. 网页抓取:PHP实现网页爬虫方式小结

    来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...

  8. Java正则表达式--网页爬虫

    网页爬虫:其实就一个程序用于在互联网中获取符合指定规则的数据 爬取邮箱地址,爬取的源不同,本地爬取或者是网络爬取 (1)爬取本地数据: public static List<String> ...

  9. 从robots.txt開始网页爬虫之旅

    做个网页爬虫或搜索引擎(下面统称蜘蛛程序)的各位一定不会陌生,在爬虫或搜索引擎訪问站点的时候查看的第一个文件就是robots.txt了.robots.txt文件告诉蜘蛛程序在server上什么文件是能 ...

随机推荐

  1. Cygwin获取root权限

    .启动cygwin进入以后,就会以真正的root权限运行了.

  2. SpriteBuilder中CCMotionStreak坐标类型不匹配

    在SpriteBuilder需要被跟随的(或是说被拖尾的)节点坐标类型是父百分比,先是将CCMotionStreak本身位置设置为百分比类型,但是无效. 将节点坐标改为正常点类型后,MotionStr ...

  3. 安卓TV开发(五) 移动智能终端UI之实现主流TV焦点可控UI

      载请标明出处:http://blog.csdn.net/sk719887916,作者:skay    由于其他网站收录,导致你无法查看本系列原创文章请点击此处 安卓TV开发(四)实现主流智能T ...

  4. rpcz VC2010 构建

    rpcz VC2010 构建 rpcz 是应用ZeroMQ和Protobuf开发的RPC. 见: https://github.com/reinferio/rpcz 及 https://code.go ...

  5. 字符转码开源库libiconv目前还不支持64位

    最新版的libiconv 1.14目前还不支持64位系统,只能编译出32位库. libiconv 1.14下载地址: http://ftp.gnu.org/pub/gnu/libiconv/libic ...

  6. PLSQL_R12 MOAC多组织的四个应用(案例)

    一.摘要 R12 Form 或者其他二次开发时,很多情况下会涉及R12 MOAC多组织开发,以下介绍了4个常见的应用,如有遗漏还请学友继续补充 1. 开发时打开Form自动弹出组织选择实现方式(增加C ...

  7. 一个 developer 的进化

    作为一名开发者已十年,回顾过往大概经历了这么几个阶段,如下图所示: Develop Code 作为刚走出学校的学生进入公司,在最初的 1-2 年内就处于该阶段. 不停的开发代码,为系统的大厦添砖加瓦, ...

  8. 2016/1/9:深度剖析安卓Framebuffer设备驱动

    忙了几天,今天在公司居然没什么活干 ,所以早上就用公司的电脑写写之前在公司编写framebuffer的使用心得体会总结,这也算是一点开发经验,不过我还没写全,精华部分还是自己藏着吧.直到下午才开始有点 ...

  9. 并发服务器--02(基于I/O复用——运用epoll技术)

    本文承接自上一博文I/O复用——运用Select函数. epoll介绍 epoll是在2.6内核中提出的.和select类似,它也是一种I/O复用技术,是之前的select和poll的增强版本. Li ...

  10. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...