Python基于队列的多线程爬虫

Wpsec-H4rdy http://www.h4rdy.me/post/1cba6492_6b61da3

Wpsec-p0di http://zone.wooyun.org/content/11888

#!/usr/bin/python
# -*- coding: utf-8 -*-
#author:Erie

import re
import sys
import requests
import Queue
import threading
from bs4 import BeautifulSoup
from urlparse import urljoin
from urlparse import urlparse
from urlparse import urlunparse
from posixpath import normpath

reload(sys)
sys.setdefaultencoding('utf-8')
sys.setdefaultencoding('gbk')

VisitedUrl = Queue.Queue()
VisitedLinks = []

#Spider Function()
class Spider(threading.Thread):
    def __init__ (self,queue,links):
        threading.Thread.__init__(self)
        tmp = urlparse(links)
        self.queue = queue
        self.host = tmp.netloc
        self.pro = tmp.scheme
        self.path = tmp.path

    #chongzu Url
    def myjoin (self,base, url):
        url1 = urljoin(base, url)
        arr = urlparse(url1)
        path = normpath(arr[2])
        return urlunparse((arr.scheme, arr.netloc, path, arr.params, arr.query, arr.fragment))
    def getpage (self,url):
        Response = requests.get(url)
        try:
            Htmlpage = Response.content
            soup = BeautifulSoup(Htmlpage)
            all_href = soup.findAll("a")
            for href in all_href:
                tmp = str(href)
                if tmp.find('href') != -1:
                    if href['href'].find("http://") != -1:
                        if urlparse(href['href']).hostname==self.host:
                            UnvisitedHref = href['href']
                            if UnvisitedHref not in VisitedLinks:
                                self.queue.put(UnvisitedHref)
                    else:
                        UnvisitedHref = self.myjoin(url,href['href'])
                        if UnvisitedHref not in VisitedLinks and urlparse(UnvisitedHref).hostname==self.host  and (urlparse(UnvisitedHref).path.count('/')-self.path.count('/'))<=3 and UnvisitedHref.find('#') == -1 and UnvisitedHref.find('.js' )== -1 and UnvisitedHref.find('.jpg') == -1 and UnvisitedHref.find('bmp') == -1 and UnvisitedHref.find('.png') == -1 and UnvisitedHref.find('.gif') == -1:
                            self.queue.put(UnvisitedHref)
        except :
            pass

    def run (self):
        while True:
            Urling =self.queue.get()
            print Urling
            VisitedLinks.append(Urling)
            self.getpage(Urling)
            self.queue.task_done()
#main Function()
def main(Url):    
    #Url = sys.argv[1]
    for i in range(5):
        spider = Spider(VisitedUrl,Url)
        spider.start()
    VisitedUrl.put(Url)
    VisitedUrl.join()
if __name__ == "__main__":
    main('http://www.sta.edu.cn')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 前些日子从@张鑫旭微博处得一份推荐(Front-end-tutorial),号称最全的资源教程-前端涉及的所有知识...
    谷子多阅读 9,759评论 0 44
  • 大部分的后端会很很鄙视前端。我也不知道为什么,可能大部分人都会觉得脚本语言根本不算语言。 大多人 会叫我们切图仔,...
    小黑的眼阅读 8,815评论 0 15
  • 清有一双特别的眼睛。 她从小就察觉到,街上来来往往的人,有的肩膀上站着一只黑色的乌鸦。 那时她一直不知道这意味着什...
    在故事里相遇阅读 3,677评论 0 0
  • 燕子楼在江苏彭城。相传是唐代名女关盼盼旧居。 宋代大文豪苏轼曾有《永遇乐》一词抒写了他夜宿燕子楼,梦见盼盼后的心...
    水乡醉客阅读 2,271评论 2 6