Scrapy has memory leak problem.
仅有的幸福
仅有的幸福 2017-06-30 09:53:22
0
1
789

When I write a crawler, it will always be OOMed after running for a while (no more than 12 hours). Very helpless! ! !
According to the official documentation, I used this prefs() but I really couldn’t find the problem.

Live References

HtmlResponse                       42   oldest: 753s ago
MySuteSpider                        1   oldest: 2964s ago
Request                         32412   oldest: 2920s ago
Selector                           42   oldest: 751s ago
TripItem                           37   oldest: 751s ago

The processing of the crawler is to obtain the links of the a tag of all pages:

#获取域名的后缀
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

#获取域名ip
def get_domain_ip(domain):
    try:
        ip = socket.gethostbyname(domain)
        return ip
    except:
        return '114.114.114.114'

# 获取域名所在的国家
def get_domain_ct_iso(ip):
    GEO = geoip2.database.Reader(
        '/var/test/geodb/GeoLite2-City.mmdb')
    r = GEO.city(ip)
    return r.country.iso_code

class 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).netloc
                if get_domain_tw(domain) or get_domain_ct_iso(get_domain_ip(domain)) == 'US':
                     yield scrapy.Request(url, callback=self.parse_items)

Please give me some advice, thank you

仅有的幸福
仅有的幸福

reply all(1)
巴扎黑

yield itemDoes it have to be implemented and saved in files or db, otherwise it will always be stored in memory

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!