簡體   English   中英

在Python 2.7中可以使用resume()函數嗎?

[英]Is resume() function available in python 2.7?

python中是否有任何resume()函數。 我需要將其應用到我的程序中。 需要適當的解釋,我進行了很多搜索,但沒有得到。

這是我需要放置resume功能的代碼。

try:
    soup = BeautifulSoup(urllib2.urlopen(url))
    abc  =  soup.find('div', attrs={})
    link =    abc.find('a')['href']
    #result is dictionary
    results['Link'] =  "http://{0}".format(link)
    print results
    #pause.minute(1)
    #time.sleep(10)

except Exception:
    print "socket error continuing the process"
    time.sleep(4)
    #pause.minute(1)
    #break

我應用了pausetime.stampbreak但沒有得到所需的結果。 如果try出現任何錯誤,那么我想pause程序。 try塊已經在循環內。

要在出現異常的情況下恢復代碼,請將其放入循環中:

import time
import urllib2
from bs4 import BeautifulSoup # $ pip install beautifulsoup4

for _ in range(max_retries):
   try:
       r = urllib2.urlopen(url) 
       encoding = r.info().getparam('charset')
       html = r.read()
   except Exception as e:
       last_error = e
       time.sleep(retry_timeout)
   else:
       break
else: # all max_retries attempts failed 
   raise last_error 

soup = BeautifulSoup(html, from_encoding=encoding)
# ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM