Python 两个刮擦蜘蛛的例子,一个内存泄漏,我可以';我找不到
这让我快发疯了。这促使我整合和简化了很多代码,但我就是无法解决这个问题。下面是我写的两个蜘蛛的例子。顶部有一个内存泄漏,导致内存缓慢扩展,直到其满 它们几乎是相同的,它们使用相同的项目和spider之外的所有东西,因此我认为我的代码的其余部分没有任何问题。我也在这里和那里分离了一些代码,最后尝试删除变量。我已经看过了那些伤痕累累的文件,但我仍然感到困惑。有人有魔法吗Python 两个刮擦蜘蛛的例子,一个内存泄漏,我可以';我找不到,python,parsing,scrapy,bots,Python,Parsing,Scrapy,Bots,这让我快发疯了。这促使我整合和简化了很多代码,但我就是无法解决这个问题。下面是我写的两个蜘蛛的例子。顶部有一个内存泄漏,导致内存缓慢扩展,直到其满 它们几乎是相同的,它们使用相同的项目和spider之外的所有东西,因此我认为我的代码的其余部分没有任何问题。我也在这里和那里分离了一些代码,最后尝试删除变量。我已经看过了那些伤痕累累的文件,但我仍然感到困惑。有人有魔法吗 import scrapy from wordscrape.items import WordScrapeItem from sc
import scrapy
from wordscrape.items import WordScrapeItem
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
import json
class EnglishWikiSpider(CrawlSpider):
name='englishwiki'
allowed_domains = ['en.wikipedia.org']
start_urls = [
'http://en.wikipedia.org/wiki/'
]
rules = (
Rule(SgmlLinkExtractor(allow=('/wiki/', )), callback='parse_it', follow=True),
)
def parse_it(self, response):
the_item = WordScrapeItem()
# This takes all the text that is in that div and extracts it, only the text, not html tags (see: //text())
# if it meets the conditions of my regex
english_text = response.xpath('//*[@id="mw-content-text"]//text()').re(ur'[a-zA-Z\'-]+')
english_dict = {}
for i in english_text:
if len(i) > 1:
word = i.lower()
if word in english_dict:
english_dict[word] += 1
else:
english_dict[word] = 1
# Dump into json string and put it in the word item, it will be ['word': {<<jsondict>>}, 'site' : url, ...]
jsondump = json.dumps(english_dict)
the_item['word'] = jsondump
the_item['site'] = response.url
return the_item
我认为杰皮奥的评论是正确的。我认为spidder发现了太多的链接,因此不得不将它们全部存储在临时perdiod中 编辑:所以,问题是它将所有这些链接存储在内存中而不是磁盘上,最终会填满我的所有内存。解决方案是使用作业目录运行scrapy,这迫使它们存储在有足够空间的磁盘上
$scrapy crawl spider-s JOBDIR=somedirname我认为杰皮奥的评论是正确的。我认为spidder发现了太多的链接,因此不得不将它们全部存储在临时perdiod中 编辑:所以,问题是它将所有这些链接存储在内存中而不是磁盘上,最终会填满我的所有内存。解决方案是使用作业目录运行scrapy,这迫使它们存储在有足够空间的磁盘上
$scrapy crawl spider-s JOBDIR=somedirname您是如何识别内存泄漏的?通过“$top”,当我运行第一个spider时,我可以看到scrapy正在缓慢扩展,直到达到最大值,当我运行第二个spider时,它稳定在2%Ish如果第一个spider只找到更多它正在寻找的内容会怎么样?我想这可能是一种可能性。我对爬行并没有太多的经验,但爬行器是否只是根据它们找到的资源数量同时发出无限数量的请求?艾德:我刚看了文档,默认的同步请求是16个。你的理论能应用于更多的请求吗?你是如何识别内存泄漏的?通过“$top”,当我运行第一个爬行器时,我可以看到scrapy慢慢扩展,直到达到最大值,当我运行第二个爬行器时,它稳定在2%的水平。如果第一个爬行器只是找到了更多它正在寻找的内容,会怎么样?我想这可能是一种可能性。我对爬行并没有太多的经验,但爬行器是否只是根据它们找到的资源数量同时发出无限数量的请求?艾德:我刚看了文档,默认的同步请求是16个。您的理论是否可以应用于不仅仅是请求?JOBDIR参数是否将请求存储到磁盘?我试过了,它创建了一些数据,但内存仍然很高。我想主要的问题是内存是否在扩展?在使用jobdir之前,它一直在膨胀,直到对机器来说太高“保持高位”是一个相对术语JOBDIR参数是否将请求存储到磁盘?我试过了,它创建了一些数据,但内存仍然很高。我想主要的问题是内存是否在扩展?在使用jobdir之前,它一直在膨胀,直到对机器来说太高“保持高位”是一个相对术语
import scrapy
from wordscrape.items import WordScrapeItem
import re
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
import json
class NaverNewsSpider(CrawlSpider):
name='navernews'
allowed_domains = ['news.naver.com']
start_urls = [
'http://news.naver.com',
'http://news.naver.com/main/read.nhn?oid=001&sid1=102&aid=0007354749&mid=shm&cid=428288&mode=LSD&nh=20150114125510',
]
rules = (
Rule(SgmlLinkExtractor(allow=('main/read\.nhn', )), callback='parse_it', follow=True),
)
def parse_it(self, response):
the_item = WordScrapeItem()
# gets all the text from the listed div and then applies the regex to find all word objects in hanul range
hangul_syllables = response.xpath('//*[@id="articleBodyContents"]//text()').re(ur'[\uac00-\ud7af]+')
# Go through all hangul syllables found and adds to value or adds key
hangul_dict = {}
for i in hangul_syllables:
if i in hangul_dict:
hangul_dict[i] += 1
else:
hangul_dict[i] = 1
jsondump = json.dumps(hangul_dict)
the_item['word'] = jsondump
the_item['site'] = response.url
return the_item