小弟在做网络爬虫程序抓取指定网页的图片,有个要求就是用户可以输入指定的爬虫深度,然后爬虫程序需达到指定深度。小弟在写的时候,程序只能处理深度为1或者深度为2的请求,我仔细看了看是我的广度优先搜索写的和深度优先搜索混合了,但不知道怎么改。请求大佬们指出问题来。下面是我的代码块:
def get_href_in_depth(url_website,depth):**#url_website是请求爬虫的网站,depth为深度
root_website=url_website
execute_list=[url_website] **#把每次执行的网页链接加到这个list里边,最开始就是请求的爬虫网站**
for i in range(int(depth)): ** #因为输入的是deepth=input("Please specify the deep you want to reach: "),需转换为integer**
for i in range(int(depth)):
for t in execute_list:
absolute_href_list=absolute_href(t) **#该方法会返回 t链接下所有的网页链接**
max_times_number=standard_length(absolute_href_list)**#该方法返回 absolute_href_list长度的众数**
for k in absolute_href_list:
if len(k.split('/'))==max_times_number and len(k.split('/'))>len(root_website.split('/'))+i :**#因为链接中有'back'会回到深度-1的页面,所以根据此排除会回到之前网页的链接**
os.chdir(os.path.split(os.path.realpath(__file__))[0])#在程序所在文件夹执行
mak_dir_html_saveimage(k)#该方法为根据网站保存目录并保存该网站下的所有图片比如http://www.feimax.com/images文件夹应该为www.feimax.com和在其目录下的images并保存相应网站的图片
else:
absolute_href_list.remove(k)#移除回到之前网站的链接
execute_list=absolute_href_list
请求大佬指出问题