【发布时间】: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