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


目录
相关文章
|
4天前
|
数据采集 JavaScript 前端开发
爬虫与反爬虫
本文介绍了爬虫与反爬虫的基本概念。爬虫是自动抓取互联网信息的程序,通常使用HTTP请求和解析技术获取数据。反爬虫技术包括验证码、User-Agent检测、IP限制、动态加载和数据接口限制等,用于阻止或限制爬虫访问。开发者需了解这些反爬虫策略,并采取相应措施应对。同时,网站运营者在实施反爬虫时也应考虑用户体验。
|
18天前
|
数据采集 Web App开发 安全
爬虫
该文介绍了爬虫的基础知识,包括爬虫的定义(通过编程模拟浏览器抓取网络数据)、价值(实际应用和就业需求)、法律地位(合法但有违法风险,分为善意和恶意爬虫)以及可能带来的风险(影响网站运营和触犯法律)。为避免问题,建议优化程序、审查抓取内容。爬虫类型包括通用、聚焦和增量式爬虫。文中还提到了反爬与反反爬策略,以及robots.txt协议作为网站数据爬取的君子协定。此外,讨论了HTTP协议(包括User-Agent和Connection)和HTTPS协议的安全性及加密方式。
15 0
|
4月前
|
数据采集 搜索推荐 数据挖掘
爬虫应用
爬虫应用
33 2
|
7月前
|
数据采集 数据安全/隐私保护 索引
爬虫学习
爬虫学习
|
数据采集 存储 JSON
一文学会爬虫技巧
作为冷数据启动和丰富数据的重要工具,爬虫在业务发展中承担着重要的作用,我们业务在发展过程中积累了不少爬虫使用的经验,在此分享给大家,希望能对之后的业务发展提供一些技术选型方向上的思路,以更好地促进业务发展
一文学会爬虫技巧
|
数据采集 消息中间件 Web App开发
爬虫介绍-queryao详细介绍|学习笔记
快速学习爬虫介绍-queryao详细介绍
136 0
爬虫介绍-queryao详细介绍|学习笔记
|
数据采集 存储 机器学习/深度学习
爬虫系列:爬虫介绍
在大数据深入人心的时代,网络数据采集作为网络、数据库与机器学习等领域的交汇点,爬虫技术已经成为满足个性化网络数据需求的最佳实践。
247 0
爬虫系列:爬虫介绍
cxa
|
数据采集 Python
一个爬虫小技巧
一个爬虫小技巧
cxa
1168 0
|
Web App开发 数据采集 JavaScript
爬虫问题总结
本文档对日常学习中用 python 做数据爬取时所遇到的一些问题做简要记录,以便日后查阅,部分问题可能因为认识不到位会存在一些误解,敬请告知,万分感谢,共同进步。 估算网站规模 该小节主要针对于整站爬取的情况。
1790 0
|
数据采集 Java 索引
gecco爬虫
http://www.geccocrawler.com/intro/ Gecco是一款用java语言开发的轻量化的易用的网络爬虫,不同于Nutch这样的面向搜索引擎的通用爬虫,Gecco是面向主题的爬虫。
946 0