python - Scrapy存在內(nèi)存泄漏的問(wèn)題。
問(wèn)題描述
再編寫爬蟲的時(shí)候,總是跑了一段時(shí)間(不會(huì)超過(guò)12個(gè)小時(shí))就會(huì)被OOM掉。很是無(wú)奈!!!根據(jù)官方的文檔, 使用這個(gè)prefs()但是實(shí)在找不出問(wèn)題的所在。
Live ReferencesHtmlResponse 42 oldest: 753s agoMySuteSpider1 oldest: 2964s agoRequest 32412 oldest: 2920s agoSelector 42 oldest: 751s agoTripItem 37 oldest: 751s ago
爬蟲的處理是獲取所有頁(yè)面的a標(biāo)簽的鏈接:
#獲取域名的后綴def get_domain_suffix(domain): if ’com’ in tldextract.extract(domain).suffix:return True return False#拼接域名。只存主域名def save_domain(domain): domain_name = tldextract.extract(domain).domain suffix_name = tldextract.extract(domain).suffix return domain_name + ’.’ + suffix_name#獲取域名ipdef get_domain_ip(domain): try:ip = socket.gethostbyname(domain)return ip except:return ’114.114.114.114’# 獲取域名所在的國(guó)家def get_domain_ct_iso(ip): GEO = geoip2.database.Reader(’/var/test/geodb/GeoLite2-City.mmdb’) r = GEO.city(ip) return r.country.iso_codeclass MyDomainSpider(scrapy.Spider): name = ’my_domain’ start_urls = [’http://xxx.com ] def parse_items(self, response):item = TripItem()for url in response.xpath(’//a/@href’).extract(): if url.startswith(’http’): domain = urlparse.urlparse(url).netloc if get_domain_tw(domain) or get_domain_ct_iso(get_domain_ip(domain)) == ’US’:item[’domain’] = save_domain(domain)item[’ip’] = get_domain_ip(domain)item[’datetime’] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')yield item def parse(self, response):for url in response.xpath(’//a/@href’).extract(): if url.startswith(’http’):domain = urlparse.urlparse(url).netlocif get_domain_tw(domain) or get_domain_ct_iso(get_domain_ip(domain)) == ’US’: yield scrapy.Request(url, callback=self.parse_items)
請(qǐng)指教一下謝謝
問(wèn)題解答
回答1:yield item 是不是得落地,存文件或者db,不然一直存內(nèi)存了
相關(guān)文章:
1. 注冊(cè)賬戶文字不能左右分離2. python - 使用readlines()方法讀取文件內(nèi)容后,再用for循環(huán)遍歷文件與變量匹配時(shí)出現(xiàn)疑難?3. 對(duì)mysql某個(gè)字段監(jiān)控的功能4. javascript - 數(shù)組的過(guò)濾和渲染5. javascript - table列過(guò)多,有什么插件可以提供列排序和選擇顯示列的功能6. html5 - ElementUI table中el-table-column怎么設(shè)置百分比顯示。7. html - vue項(xiàng)目中用到了elementUI問(wèn)題8. showpassword里的this 是什么意思?代表哪個(gè)元素9. python - 為什么正常輸出中文沒有亂碼,zip函數(shù)之后出現(xiàn)中文編程unicode編碼的問(wèn)題,我是遍歷輸出的啊。10. JavaScript事件
