Python脚本在几分钟后停止

时间:2016-10-05 13:26:44

标签: python

我建立一个python脚本,每隔5-10秒检查亚马逊物品的价格。问题是,脚本停止工作'几分钟后。控制台没有输出,但它显示为'正在运行'在我的过程中。

我使用请求会话来发出http请求和时间来显示请求的时间。

我的代码如下;

target_price = raw_input('Enter target price: ')
url = raw_input('Enter the product url: ')
while True:
    delay=randint(5,10)

    print datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')+': '+'Sleeping for ' + str(delay) + ' seconds'
    time.sleep(delay)
    try:
        with requests.Session() as s:
            page = s.get(url,headers=headers,proxies=proxyDict,verify=False,timeout=5)
            tree = html.fromstring(page.content)
            price = tree.xpath('//div[@class="a-row a-spacing-mini olpOffer"]/div[@class="a-column a-span2 olpPriceColumn"]/span[@class="a-size-large a-color-price olpOfferPrice a-text-bold"]/text()')[0]
            new_price = re.findall("[-+]?\d+[\.]?\d+[eE]?[-+]?\d*", price)[0]
            old_price = new_price
            print new_price
            if float(new_price)<float(target_price):
                print 'Lower price found!'
                mydriver = webdriver.Chrome()
                send_simple_message()
                login(mydriver)
                print 'Old Price: ' + old_price
                print 'New Price: ' + new_price
            else:
                print 'Trying again'
    except Exception as e:
        print e
        print 'Error!'

编辑:我已经删除了wait()函数并改为使用time.sleep。

EDIT2:当我使用键盘中断来停止脚本时,输出

    Traceback (most recent call last):
  File "checker.py", line 85, in <module>
    page = s.get(url,headers=headers,proxies=proxyDict,verify=False,timeout=5)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 488, in get
return self.request('GET', url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 423, in send
timeout=timeout
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 589, in urlopen
self._prepare_proxy(conn)
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 797, in _prepare_proxy
conn.connect()
  File "C:\Python27\lib\site-packages\requests\packages\urllib3\connection.py",
line 267, in connect
self._tunnel()
  File "C:\Python27\lib\httplib.py", line 729, in _tunnel
line = response.fp.readline()
KeyboardInterrupt

请求是否正在进入无限循环?

1 个答案:

答案 0 :(得分:1)

s.get()函数的超时参数很棘手。 Here我找到了一个很好的解释,因为它的异常行为。如果请求的网址没有响应,timeout将停止进程,但如果响应无限,则不会停止。

在您的情况下,建立连接,所请求的页面只是在无限循环中发送响应。

您可以为整个函数调用设置超时:Timeout function if it takes too long to finish