通过任务管理器运行的Python脚本在注销Windows约30分钟后死亡

时间:2016-12-13 15:57:41

标签: python beautifulsoup taskscheduler

我设置了Windows电源设置,以便我的计算机永远不会关闭,永远不会进入睡眠状态。我已经设置了以下python脚本作为计划任务运行,运行正常,直到我退出计算机后差不多半小时。它神秘地停止了。事件日志中没有错误消息。此过程使用的内存似乎并不显着。我不知道发生了什么事。请帮忙!

import datetime
from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
import logging
import shutil

def get_soup(theurl):
    html = urlopen(theurl)
    return BeautifulSoup(html.read(), "lxml")

def output_data(soup, filename, fieldnames, rows_with_info, supervision_row_count):
    with open(filename, 'a', newline='') as csvfile:
        mywriter = csv.DictWriter(csvfile, fieldnames=fieldnames)

        mydict = {};

        # Scraping first table         
        offender_table = soup.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']=="offender-search-details")
        rows = offender_table.findAll(lambda tag: tag.name=='tr')
        mydict[fieldnames[0]]= clean_data(name_and_id[0])
        mydict[fieldnames[1]]= clean_data(name_and_id[1])          

        #lots of similar code removed for sake of brevity

        mywriter.writerow(mydict)   


start_id = 10
max_id = 199999

for the_id in range(start_id, max_id):
    logger.info('running with id-' + str(the_id))

    theurl=thebaseurl + str(the_id)

    soup = get_soup(theurl)
    sentence_table = soup.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']=="offender-search-sentence-info")

    if sentence_table:
        logger.info('found data for id-' + str(the_id))

        sentence_rows = sentence_table.findAll(lambda tag: tag.name=='tr')

        supervision_row_count = 0

        for the_row_index in range(0, len(sentence_rows)):
            col_count = sentence_rows[the_row_index].find_all('td')
            if (len(col_count) == 2):
                supervision_row_count = supervision_row_count + 1

        supervision_row_count = supervision_row_count -1
        rows_with_info = len(sentence_rows) - 4 - supervision_row_count 

        output_data(soup, filename, fieldnames, rows_with_info, supervision_row_count)

logger.info('finished-' + str(datetime.datetime.now()))

0 个答案:

没有答案