Python script stops after a few minutes
我正在构建一个python脚本,每隔5-10秒检查一个亚马逊产品的价格。问题是,脚本几分钟后停止"工作"。控制台没有输出,但在我的进程中显示为"正在运行"。
我使用请求会话进行HTTP请求,并使用时间显示请求时间。
我的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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:当我使用键盘中断来停止脚本时,这里是输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 equests\sessions.py", line 488, in get return self.request('GET', url, **kwargs) File"C:\Python27\lib\site-packages equests\sessions.py", line 475, in request resp = self.send(prep, **send_kwargs) File"C:\Python27\lib\site-packages equests\sessions.py", line 596, in send r = adapter.send(request, **kwargs) File"C:\Python27\lib\site-packages equests\adapters.py", line 423, in send timeout=timeout File"C:\Python27\lib\site-packages equests\packages\urllib3\connectionpool.py", line 589, in urlopen self._prepare_proxy(conn) File"C:\Python27\lib\site-packages equests\packages\urllib3\connectionpool.py", line 797, in _prepare_proxy conn.connect() File"C:\Python27\lib\site-packages equests\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 |
是否请求正在进入无限循环?
在您的情况下,连接是建立的螺母,请求的页面只是在无限循环中发送响应。
如果完成时间过长,可以为整个函数调用设置超时:timeout函数