【问题标题】:How to add multithreading in the Spider Crawler in python?如何在 python 的 Spider Crawler 中添加多线程?
【发布时间】:2016-03-08 00:02:01
【问题描述】:

我使用 BeautifulSoup 和 urllib2 编写了一个蜘蛛爬虫。这会将所有链接解析为 2 级,并将所有 html 页面收集到一个列表中。我尝试将其设为多线程以加快 Spidering 进程的速度,但不知道从哪里开始?

下面是代码。

#
#!/usr/bin/python
from bs4 import BeautifulSoup
import time
import urllib2
import sys

masterList = []
masterList1 = []
htmlList = []
url = "http://www.securitytube.net"
dictList = []

def spidy(url):
            try:
                    page = urllib2.urlopen(url)

                    soup = BeautifulSoup(page.read())
                    if soup:

                            for links in soup.findAll('a',href=True):

                                    ele = links['href']

                                    if ".html" in ele and "http://" in ele:
                                            htmlList.append(ele)
                                            print ele


                                    elif ".html" in ele and "https://" in ele:
                                            htmlList.append(ele)
                                            print ele

                                    else:
                                            masterList.append(ele)


                    for ele in masterList:
                            if 'mailto:' in ele:
                                    masterList.remove(ele)

            except:
                    print "url %s is not accessible ... Moving on to the next URL .."%(url)
                    pass

def level():
    masterList1 = list(set(masterList))
    for url1 in masterList1:

            print "Running Spidy on : %s"%(url1)
            print "\n########################################################\n"

            spidy(url1)

            print "\n########################################################\n"
            masterList1.remove(url1)


            masterList.remove(url1)

def main():
    spidy("http://www.securitytube.net")
    level()
    level()
    print "\n\n\n\n\n********************************************************************"
    print htmlList

【问题讨论】:

    标签: multithreading python-2.7 beautifulsoup web-crawler urllib2


    【解决方案1】:

    你听说过Scrapy吗?

    用它创建一个简单的蜘蛛非常容易,人们已经开发了多年。它不完全是多线程的,但它在后台使用 Twisted,因此它是完全异步和基于事件的。

    【讨论】:

    • 我去看看。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多