python类方法__del__ with xlsxwriter.workbook.close()

时间:2018-03-15 02:43:04

标签: python xlsxwriter del

class house_crawl():
def __init__(self):
    self.row=0
    self.workbook = xlsxwriter.Workbook(self.getdesktoppath() + '\\house_price.xlsx')
    self.worksheet1 = self.workbook.add_worksheet()
    self.worksheet1.write_row(self.row,0,['address','describe1','describle2','price','avg_price'])
    self.row+=1
def __call__(self, url):
    self.page_crawl(url)
def __del__(self):
    self.workbook.close()
def page_crawl(self,url):
    page=requests.get(url)
    tree=html.fromstring(page.text)
    for house in tree.xpath('//*[@class="house-item clearfix"]'):
        address=house.xpath('div[1]/p[3]/text()')[0]
        address=re.sub('\r|\n| ','',address)
        #print(address)浦东-潍坊崂山路571弄(旧址崂山东路571弄)
        d1=[]
        for i in house.xpath('div[1]/p[1]/child::*'):
            d1.append(i.xpath('text()')[0])
        st=','
        d1=st.join(d1)
        # print(d1)东欣高层,3室1厅,70平
        d2=house.xpath('div[1]/p[2]/text()')
        stringt=','
        d2=stringt.join(d2)
        d2 = re.sub('\r|\n| ','',d2)
        #print(d2)南,中层,中装,1991年
        price=house.xpath('div[2]/p[1]/text()')[0]
        #print(price)580万
        avg_price=house.xpath('div[2]/p[2]/text()')[0]
        if '元/平' not in avg_price:
            avg_price = house.xpath('div[2]/p[3]/text()')[0]
        #print(avg_price)83400元/平
        self.worksheet1.write_row(self.row, 0, [address,d1,d2,price,avg_price])
        self.row+=1
def getdesktoppath(self):
    return os.path.join(os.path.expanduser("~"), 'Desktop')
抛出异常是:

Exception ignored in: <bound method house_crawl.__del__ of <__main__.house_crawl object at 0x000001A14533B940>>
Traceback (most recent call last):
  File "D:/pythoncode/crawl/house.py", line 15, in __del__
  File "C:\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", line 311, in close
  File "C:\Anaconda3\lib\site-packages\xlsxwriter\workbook.py", line 619, in _store_workbook
  File "C:\Anaconda3\lib\site-packages\xlsxwriter\packager.py", line 131, in _create_package
  File "C:\Anaconda3\lib\site-packages\xlsxwriter\packager.py", line 188, in _write_worksheet_files
  File "C:\Anaconda3\lib\site-packages\xlsxwriter\xmlwriter.py", line 41, in _set_xml_writer
  File "C:\Anaconda3\lib\codecs.py", line 895, in open
AttributeError: module 'builtins' has no attribute 'open'

它表示&#39; self.workbook.close()&#39;在 del 中是错误的。 我不明白....... 请帮帮我!

0 个答案:

没有答案