我使用goose-extractor从字典键中解析urls列表。我使用的是python 2.7.6,我的代码如下:
import json
import re
import sys
from goose import Goose
from pymongo import MongoClient
mongoobj = MongoClient()
coll = mongoobj.db_name.coll_name
gobj = Goose()
eachkey = sys.argv[1]
print "\n "+eachkey
all_data = []
total_data = len(json_data[eachkey])
count = 0.0
for each_link in json_data[eachkey]:
print "\r",str(round(count/total_data,2)),
count += 1
try:
data = gobj.extract(each_link)
new_data =" ".join( re.findall(r"\b\w+\b",data.cleaned_text))
text = ""
if new_data:
text = new_data
elif data.meta_description:
text = " ".join(re.findall(r"\b\w+\b", data.title + " " + data.meta_description))
if text:
coll.insert_one({"text":text, "label":eachkey, "title":data.title})
except Exception as e:
print eGoose似乎在tmp中创建了一个名为goose的文件夹,它正在用tmp文件填充这个文件夹,它已经填满了我的系统空间。我不希望这件事让我的系统崩溃。有没有什么地方我做错了,垃圾收集没有正确发生。
发布于 2019-01-15 14:45:39
Goose将数据临时存储在本地存储上,然后才能从本地存储中提取内容,我不知道有什么爬虫可以在不使用一定数量的本地存储的情况下有效地完成这项工作。
可以从文件configuration.py as self.local_storage_path =配置本地存储路径。也在该文件集self.debug =False中,以避免进一步加载。
同样,在goose完成之后,使用评论中建议的release_resources()函数,它看起来像这样
def relase_resources(self):
path = os.path.join(self.config.local_storage_path, '%s_*' % self.article.link_hash)
for fname in glob.glob(path):
try:
os.remove(fname)
except OSError:
# TODO better log handling
pass并清除临时资源。
通常,即使只有几百个中层源代码的tmp文件也不足以使系统崩溃。
https://stackoverflow.com/questions/50000758
复制相似问题