python的正則與網頁操做練習二:html
import re import urllib.request #51cto urlcode=gb18030 class down51web: s_url='' s_blogid='' s_blogpages='' s_html='' s_code='' def __init__(self,url,code): self.s_url=url self.s_code=code def get_html(self): self.s_html=urllib.request.urlopen(self.s_url).read().decode(self.s_code) return self.s_html def get_page(self,r_page): if len(self.s_html) > 0: m_pages=r_page.search(self.s_html) if m_pages: self.s_blogpages=m_pages.group(1) return self.s_blogpages def get_blogid(self,r_blogid): if len(self.s_html) > 0: m_blogid=r_blogid.search(self.s_html) if m_blogid: self.s_blogid=m_blogid.group(1).split('/')[1] return self.s_blogid def get_blogpagelist(self): bloglist=[] if len(self.s_blogid)>0 and len(self.s_blogpages)>0: for i in range(1,int(self.s_blogpages)+1): bloglist.append(self.s_url+'/'+self.s_blogid+'/p-'+str(i)) return bloglist def get_pagelist(self,r_list,url): bloglist=[] self.s_url=url s_tmphtml=self.get_html() if len(s_tmphtml) > 0: bloglist=r_list.findall(s_tmphtml) return bloglist r_page=re.compile('頁數 \( [0-9]+/([0-9]+) \)(?=</div>)') r_blogid=re.compile('(?<=<div class="pages">)<a href=([^>]*)>') r_list=re.compile('<h3 class="artTitle"><a href="([^"]*)">([^<]*)</a>') xx='http://hxw168.blog.51cto.com' hxw=down51web(xx,'gb18030') hxw.get_html() #print(hxw.s_html) print(hxw.get_page(r_page)) print(hxw.get_blogid(r_blogid)) list=hxw.get_blogpagelist() for i in list: l1=hxw.get_pagelist(r_list,i) for x in l1: print(xx+x[0]+'-------'+x[1])
代碼排版不正常python
import re import urllib.request #51cto urlcode=gb18030 class down51web: s_url='' s_blogid='' s_blogpages='' s_html='' s_code='' def __init__(self,url,code): self.s_url=url self.s_code=code def get_html(self): self.s_html=urllib.request.urlopen(self.s_url).read().decode(self.s_code) return self.s_html def get_page(self,r_page): if len(self.s_html) > 0: m_pages=r_page.search(self.s_html) if m_pages: self.s_blogpages=m_pages.group(1) return self.s_blogpages def get_blogid(self,r_blogid): if len(self.s_html) > 0: m_blogid=r_blogid.search(self.s_html) if m_blogid: self.s_blogid=m_blogid.group(1).split('/')[1] return self.s_blogid def get_blogpagelist(self): bloglist=[] if len(self.s_blogid)>0 and len(self.s_blogpages)>0: for i in range(1,int(self.s_blogpages)+1): bloglist.append(self.s_url+'/'+self.s_blogid+'/p-'+str(i)) return bloglist def get_pagelist(self,r_list,url): bloglist=[] self.s_url=url s_tmphtml=self.get_html() if len(s_tmphtml) > 0: bloglist=r_list.findall(s_tmphtml) return bloglist r_page=re.compile('頁數 \( [0-9]+/([0-9]+) \)(?=</div>)') r_blogid=re.compile('(?<=<div class="pages">)<a href=([^>]*)>') r_list=re.compile('<h3 class="artTitle"><a href="([^"]*)">([^<]*)</a>') xx='http://hxw168.blog.51cto.com' hxw=down51web(xx,'gb18030') hxw.get_html() #print(hxw.s_html) print(hxw.get_page(r_page)) print(hxw.get_blogid(r_blogid)) list=hxw.get_blogpagelist() for i in list: l1=hxw.get_pagelist(r_list,i) for x in l1: print(xx+x[0]+'-------'+x[1])
結果以下:linux
>>> (executing lines 1 to 74 of "downweb.py") 4 8718136 http://hxw168.blog.51cto.com/8718136/1533205-------【python】簡單的網頁內容獲取 - 有道翻譯英文 http://hxw168.blog.51cto.com/8718136/1440229-------【文本處理】awk、sed使用 - 二 http://hxw168.blog.51cto.com/8718136/1436143-------【文本處理】sed的理解工具sedsed http://hxw168.blog.51cto.com/8718136/1435310-------【文本處理】awk、sed使用 - 一 http://hxw168.blog.51cto.com/8718136/1424626-------【shell】oracle安裝前環境設置 http://hxw168.blog.51cto.com/8718136/1424422-------【oracle】Oracle12c安裝及一些使用問題 http://hxw168.blog.51cto.com/8718136/1419971-------【oracle】系統權限、對象權限、角色 http://hxw168.blog.51cto.com/8718136/1419736-------【oracle】模擬故障 - 參數修改致使沒法啓動oracle http://hxw168.blog.51cto.com/8718136/1419733-------【oracle】表空間 http://hxw168.blog.51cto.com/8718136/1419236-------【oracle】數據庫啓動 http://hxw168.blog.51cto.com/8718136/1410362-------【1】oracle視頻 - 流水賬筆記 http://hxw168.blog.51cto.com/8718136/1410179-------【基礎服務】dns智能解析、view主從服務 -.. http://hxw168.blog.51cto.com/8718136/1409219-------【基礎服務】簡單理解DNS的遞歸、迭代查詢.. http://hxw168.blog.51cto.com/8718136/1408946-------【基礎應用】rsync簡單使用 http://hxw168.blog.51cto.com/8718136/1408462-------【6】iptables理解 - 淺嘗輒止 http://hxw168.blog.51cto.com/8718136/1408295-------【5】iptables理解 - mangle表 http://hxw168.blog.51cto.com/8718136/1405101-------【4】iptables理解 - nat表 http://hxw168.blog.51cto.com/8718136/1404942-------【3】iptables理解 - filter表 http://hxw168.blog.51cto.com/8718136/1404541-------【2】iptables理解 - 整體印象 http://hxw168.blog.51cto.com/8718136/1404463-------【1】iptables理解 - 練習環境搭建 http://hxw168.blog.51cto.com/8718136/1401483-------【5】puppet筆記 - defined類型、erb模板 http://hxw168.blog.51cto.com/8718136/1397872-------【4】puppet筆記 - 第一個簡單模塊 http://hxw168.blog.51cto.com/8718136/1396483-------【4】puppet筆記 - 數組、類 http://hxw168.blog.51cto.com/8718136/1396308-------【3】puppet筆記 - 變量、if、case、selector http://hxw168.blog.51cto.com/8718136/1395869-------【2】puppet筆記 - package、service、use.. http://hxw168.blog.51cto.com/8718136/1395357-------【1】puppet筆記 - file資源 http://hxw168.blog.51cto.com/8718136/1395073-------【基礎】yum本地源 - yum puppet-server爲例 http://hxw168.blog.51cto.com/8718136/1394312-------【python】操做oracle數據庫 http://hxw168.blog.51cto.com/8718136/1394303-------【基礎】LVM簡單使用 - 擴展縮小LV容量 http://hxw168.blog.51cto.com/8718136/1394089-------【基礎】LVM簡單使用 - 一 http://hxw168.blog.51cto.com/8718136/1393463-------【基礎】linux添加新硬盤 http://hxw168.blog.51cto.com/8718136/1390593-------【8】nagios從零學習使用 - 編寫本身的nag.. http://hxw168.blog.51cto.com/8718136/1390357-------【7】nagios從零學習使用 - nrpe插件使用 http://hxw168.blog.51cto.com/8718136/1389588-------【6】nagios從零學習使用 - centreon發送.. http://hxw168.blog.51cto.com/8718136/1388191-------【5】nagios從零學習使用 - centreon使用c.. http://hxw168.blog.51cto.com/8718136/1387516-------【4】nagios從零學習使用 - centreon安裝過程 http://hxw168.blog.51cto.com/8718136/1382079-------【3】nagios從零學習使用 - 監控 win主機 http://hxw168.blog.51cto.com/8718136/1380326-------【2】nagios從零學習使用 - cacti作圖 http://hxw168.blog.51cto.com/8718136/1379795-------【shell】使用ping檢測機器是否不在線 http://hxw168.blog.51cto.com/8718136/1379586-------【1】nagios從零學習使用 - 軟件安裝 >>>