<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: freebuf爬虫#C:\Python27\python.exe#coding:utf-8import sysreload(sys)sys.

freebuf爬虫

#C:\Python27\python.exe
#coding:utf-8

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

import re
import os
import urllib
import requests
from multiprocessing import Pool


subject_dict = {u'漏洞':'http://www.freebuf.com/vuls', u'安全工具':'http://www.freebuf.com/sectool',
                u'WEB安全':'http://www.freebuf.com/articles/web', u'系统安全':'http://www.freebuf.com/articles/system',
                u'网络安全':'http://www.freebuf.com/articles/network', u'无线安全':'http://www.freebuf.com/articles/wireless',
                u'终端安全':'http://www.freebuf.com/articles/terminal', u'数据安全':'http://www.freebuf.com/articles/database',
                u'安全管理':'http://www.freebuf.com/articles/security-management', u'企业安全':'http://www.freebuf.com/articles/es',
                u'极客':'http://www.freebuf.com/geek'}


def spider(filename, url):
    print "Crawling subject: %s" % filename
    if os.path.isfile(filename + ".html"):
        os.remove(filename + ".html")
    with open(filename + ".html",'a') as f:
        page = 0
        error_couter = 0
        while True:
            page += 1
            try:
                html = requests.get(url + '/page/' + str(page))
                code = html.status_code
                if code == 404:
                    error_couter += 1
                    if error_couter == 1:
                        print "Subject %s may only have %s pages." % (filename, str(page - 1))
                    if error_couter <= 3:
                        print "Retrying %s: 404 not Found!" % str(error_couter)
                        continue
                    else:
                        print "Subject %s finished!" % filename
                        print "#################################"
                        break
                else:
                    print u"Parsing page: " + str(page)
                    if page == 1:
                        site = re.findall('([\s\S]*)      </div>\n      <div class="news-more" id="pagination">',html.text,re.S)
                    else:
                        site = re.findall('<div id="timeline" class="news-detial">([\s\S]*?)      </div>\n      <div class="news-more" id="pagination">',html.text,re.S)
                    for each in site:
                        f.write(urllib.unquote(each.encode('utf-8')))
            except Exception as e:
                print e
                pass
        f.close()


def main():
    for key,value in subject_dict.items():
        spider(key, value)

    # pool = Pool(processes=4)
    # for i in range(0, subject_dict.__len__()):
    #     arg_list = subject_dict.items()[i]
    #     pool.apply_async(spider, (arg_list[0], arg_list[1],)).get(timeout=None)
    # pool.close()
    # pool.join()


if __name__ == '__main__':
    main()


目录
打赏
0
0
0
0
11
分享
相关文章
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
可伸缩系统的架构经验 Feb 27th, 2013 | Comments 最近,阅读了Will Larson的文章Introduction to Architecting System for Scale,感觉很有价值。
2363 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一个典型的星型模式包括一个大型的事实表和一组逻辑上围绕这个事实表的维度表。  事实表是星型模型的核心,事实表由主键和度量数据两部分组成。
574 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
各大互联网公司架构演进之路汇总 大型网站架构演化历程 大型网站架构技术一览 Web 支付宝和蚂蚁花呗的技术架构及实践 支付宝的高可用与容灾架构演进 聚划算架构演进和系统优化 (视频+PPT) 淘宝交易系统演进之路 (专访) 淘宝数据魔方技术架构解析 淘宝技术发展历程和架构经验分享(视频+PPT)(2.
2151 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
1.样本要随机化,防止大数据淹没小数据 2.样本要做归一化。关于归一化的好处请参考:为何需要归一化处理3.激活函数要视样本输入选择(多层神经网络一般使用relu)4.
740 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在使用Spark Streaming过程中,对foreachRDD有点疑问,查阅资料后记录如下: foreachRDD(func)的官方解释为 The most generic output operator ...
831 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一般来说,Linux的虚拟内存会根据系统负载自动调整。内存页(page)swap到磁盘会显著的影响Kafka的性能,并且Kafka重度使用page cache,如果VM系统swap到磁盘,那说明没有足够的内存来分配page cache。
696 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
Jboss7中ejb3使用@Schedule调度器总是每分钟执行 1 问题:当我尝试着去开发一个ejb3的@Schedule调度器来执行我预定每5秒钟执行一次的任务时。
791 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一 概要说明 使用nginx搭建流媒体直播平台,目的就是要支持rtmp协议,实现用户使用rtmp(rtmp://192.168.201.128/myapp)协议推送流到服务器。
1722 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等