从网上获取目标网站的数据一般通过网络爬虫的方式,但是这种方式往往可能效率比较低,而且有些网站可能会限制爬虫,比如著名的Github,当通过API爬虫的方式获取数据的时候,Github官方对爬虫速率进行了限制,超过了这个速率的爬虫会被限制,同时Github官方还要求提供Github用户验证等等,这些限制无形中都加大了我们爬虫的难度,但是其实我们还可以通过直接下载Github仓库的方式来获取数据,这种方式比爬虫要更加方便快捷,但是下下来的文件中很多是文件夹或者不符合条件的文件,我们该如何快速的通过python脚本将这些文件提取出来呢?下面就是我们要介绍的使用python递归遍历目标路径下的所有文件。
import os import json target_json = []
def getTarget(dir): temp = os.listdir(dir) for t in temp: if (os.path.isfile(dir + '/' + t)): print('找到了',t) for line in open(dir + '/' + t): # print(line) line=line.strip('\n') line = json.loads(line) target_json.append(line) # with open(dir + '/' + t) as f: # line = f.readline() # while line: # target_json.append(line) # print(line) else: getTarget(dir + '/' + t) getTarget('./crates.io-index-master')
json_str = json.dumps(target_json) with open('target_json.json', 'w') as json_file: json_file.write(json_str)
获取有代码仓库的网站的数据,不一定要使用爬虫,有时候直接从代码仓库中下载数据并使用脚本进行处理,无论是从获取的速度还是精度都要更高。
下一个:正则表达式贪婪与非贪婪模式