开发者社区> 神棍先生> 正文

FreeBuf爬虫

简介: 版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.
+关注继续查看
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/77673417

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()


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
一文学会爬虫技巧
作为冷数据启动和丰富数据的重要工具,爬虫在业务发展中承担着重要的作用,我们业务在发展过程中积累了不少爬虫使用的经验,在此分享给大家,希望能对之后的业务发展提供一些技术选型方向上的思路,以更好地促进业务发展
80 0
爬虫介绍-queryao详细介绍|学习笔记
快速学习爬虫介绍-queryao详细介绍
68 0
爬虫学习:XPath的使用
熟知XPath的使用来进行HTML信息锁定并获取
98 0
爬虫系列:爬虫介绍
在大数据深入人心的时代,网络数据采集作为网络、数据库与机器学习等领域的交汇点,爬虫技术已经成为满足个性化网络数据需求的最佳实践。
159 0
python爬虫系列之初识爬虫
python爬虫系列之初识爬虫前言 我们这里主要是利用requests模块和bs4模块进行简单的爬虫的讲解,让大家可以对爬虫有了初步的认识,我们通过爬几个简单网站,让大家循序渐进的掌握爬虫的基础知识,做网络爬虫还是需要基本的前端的知识的,下面我们进行我们的爬虫讲解 在进行实战之前,我们先给大家看下...
1480 0
cxa
一个爬虫小技巧
一个爬虫小技巧
1057 0
爬虫问题总结
本文档对日常学习中用 python 做数据爬取时所遇到的一些问题做简要记录,以便日后查阅,部分问题可能因为认识不到位会存在一些误解,敬请告知,万分感谢,共同进步。 估算网站规模 该小节主要针对于整站爬取的情况。
1657 0
gecco爬虫
http://www.geccocrawler.com/intro/ Gecco是一款用java语言开发的轻量化的易用的网络爬虫,不同于Nutch这样的面向搜索引擎的通用爬虫,Gecco是面向主题的爬虫。
877 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载