python class method __del__ with xlsxwriter.workbook.close()


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:Anaconda3libsite-packagesxlsxwriterworkbook.py", line 311, in close
  File "C:Anaconda3libsite-packagesxlsxwriterworkbook.py", line 619, in _store_workbook
  File "C:Anaconda3libsite-packagesxlsxwriterpackager.py", line 131, in _create_package
  File "C:Anaconda3libsite-packagesxlsxwriterpackager.py", line 188, in _write_worksheet_files
  File "C:Anaconda3libsite-packagesxlsxwriterxmlwriter.py", line 41, in _set_xml_writer
  File "C:Anaconda3libcodecs.py", line 895, in open
AttributeError: module 'builtins' has no attribute 'open'

它说del中的"self.workbook.close(("是错误的。我不明白。。。。。。。请帮助我!

2022-07-06 20:02:33 
2022-07-06 20:02:33 Traceback (most recent call last):
2022-07-06 20:02:33   File "/usr/lib/python3.8/encodings/__init__.py", line 31, in <module>
2022-07-06 20:02:33   File "/var/www/codecs.py", line 14, in <module>
2022-07-06 20:02:33 AttributeError
2022-07-06 20:02:33 : 
2022-07-06 20:02:33 module 'builtins' has no attribute 'open'
2022-07-06 20:02:33 

最新更新