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. Gradle 1.12用户指南翻译——第三十八章. Eclipse 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  2. Erlang cowboy http request生命周期

    Erlang cowboy http request生命周期 翻译自: http://ninenines.eu/docs/en/cowboy/1.0/guide/http_req_life/ requ ...

  3. obj-c中SEL签名和Invocation示例

    参考小示例,代码如下: #import <Foundation/Foundation.h> @interface PlayList:NSObject @property NSMutable ...

  4. C语言实现printf的基本格式输出%d,%c,%p,%s

    关于printf的实现,想必看过我之前发表的文章的伙伴们已经了解了不少基本的知识.好了,接下来不多说了,直接上源码,看看一种简单的实现方式: #include <stdio.h> #def ...

  5. 基于MT6752/32平台 Android L版本驱动移植步骤

    基于MT6752/32平台 Android L版本驱动移植步骤 根据MK官网所述,在Android L 版本上Turnkey ABS 架构将会phase out,而Mediatek Turnkey架构 ...

  6. 【42】android Context深度剖析

    android程序和java程序的区别 Android程序不像Java程序一样,随便创建一个类,写个main()方法就能跑了,而是要有一个完整的Android工程环境,在这个环境下,我们有像Activ ...

  7. N-Queens(N皇后问题)

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  8. CSDN的博客搜索功能不又给力了呵呵呵呵

    不得不说,CSDN博客的搜索功能是在太弱了.而且一直都很弱,以至于我每次想在自己博客上找自己发的文章都变得那么难.做一个搜索博客内文章的功能没有那么难吧? 还是说CSDN已经放弃了博客这一块了? 我发 ...

  9. Airbnb/Apache Superset – the open source dashboards and visualization tool – first impressions and link to a demo

    https://assemblinganalytics.com/post/airbnbapache-superset-first-impressions-and-link-to-a-demo/ Tod ...

  10. 使用springmvc时静态的文件获取不到,比如说样式丢失的问题。

    当使用springmvc时前台所有的样式全部都消失不见了,查了很多资料,简单的说就是我在配置web.xml中的过滤器时将<url-pattern></url-pattern>中 ...