为什么这个简短的Python代码使用越来越多的内存?

时间:2017-11-02 18:28:14

标签: python memory-leaks screen-scraping

我用这个简短的脚本来检查亚马逊猫砂的价格,如果它低于9美元,请提醒我。它每六个小时检查一次。该脚本以9 MB的速度启动,但在几天内以某种方式爬升到16 MB。

from random import random
from requests import get
from time import sleep
from datetime import datetime

def ReadAsin():
    def find_item(text, start, end):
        start_index = text.index(start)
        end_index = text[start_index + len(start):].index(end)
        return text[start_index + len(start):][:end_index]

    headers = {
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'}
    AsinList = ['B001N02JXW','B01N9VEMP6']
    # extracted_data = []

    while True:
        for i in AsinList:
            url = "http://www.amazon.de/dp/"+i
            # print("Processing: ",url)
            r = get(url,headers=headers)
            sleep(5 * random())
            try:
                title = ''
                price = find_item(r.text, '<span id="priceblock_ourprice" class="a-size-medium a-color-price">EUR ', '</span>')
                title = find_item(r.text, '<meta name="title" content=', 'Amazon.de')
            except:
                pass
            else:
                price = float(price.replace(',','.'))
                if price < 9:
                    now = datetime.now()
                    print(now.strftime("%Y-%m-%d %H:%M:%S"),': ',title,'EUR',price)
        sleep(60 * 60 * 6)

if __name__ == "__main__":
    ReadAsin()

0 个答案:

没有答案