#-*-coding:utf-8-*- fromBs4ImportBeautifulSoupImportRequestsImportSYSclassDownLoader (object):def __init__(self): Self.server='http://www.biqukan.com/'Self.target='http://www.biqukan.com/0_790/'Self.header= {'user-agent':'mozilla/5.0 (Windows NT 6.1; Win64; x64) APPLEWEBK' 'it/537.36 (khtml, like Gecko) chrome/64.0.3282.119 safari/537.36'} self.name=[] Self.urls=[] Self.num=0defGet_downloader_url (self): req= Requests.get (Url=self.target, headers=self.header) HTML=Req.text DIV_BF= BeautifulSoup (HTML,'lxml') Div= Div_bf.find_all ('Div', class_='Listmain') A_BF= BeautifulSoup (str (div[0]),'lxml') A= A_bf.find_all ('a') Self.num= Len (a[16:]) forIinchA[16:]: Self.name.append (i.string) self.urls.append (Self.server+i.get ('href')) defget_content (self, target): Req= Requests.get (Target, headers=self.header) HTML=Req.text CON_BF= BeautifulSoup (HTML,'lxml') Con= Con_bf.find_all ('Div', class_='Showtxt') Con= Con[0].text.replace ('\xa0'*8,'\ n') returnCondefwriter (self, name, path, text): Write_flag=True with open (path,'a', encoding='Utf-8') as F:f.write (name+'\ n') f.writelines (text) f.write ('\ n')if __name__=='__main__': DL=DownLoader () Dl.get_downloader_url ( )Print('"Yuan Zun" began to download:') forIinchRange (Dl.num): Dl.writer (Dl.name[i],'Yuan Zun. txt', Dl.get_content (dl.urls[i)) Sys.stdout.write ("downloaded:%.3f"% Float (i/dl.num) +'\ r') Sys.stdout.flush ()Print('Download Complete')
Python Crawl Pen Interesting reading novel