要點:
1.使用 生成器 yield 減少記憶體消耗;
2.使用正則匹配關鍵字提取符合規則的檔案標題;
import requests
from lxml import etree
import re
import csv
urls=
['',''
,'',''
,'']headers=
class
youzheng
(object):
def__init__
(self,urls)
: self.urls=urls
#self.file=file
defget_response
(self,url)
: html=requests.get(url,headers=headers)
result=html.content.decode(
'utf-8'
)return result
defparse_response
(self,result)
:'''
提取網頁資訊
result::接收的response
'''html=etree.html(result)
l_h=html.xpath(
'//table[@id="submenu111"]/tbody/tr'
)for l in l_h:
d1=d1[
'title'
]=l.xpath(
'td[@align="left"]/a/@title')[
0]d1[
'href']=
''+l.xpath(
'td[@align="left"]/a/@href')[
0].strip(
'.')
yield d1
defremove_duplicate
(self,d1)
:'''
過濾符合條件的標題
d1::包含**資訊的字典
return::符合要求的字典資訊
'''pattern=re.
compile
('\d年\d月.*?運**況'
) title=d1[
'title'
]if re.match(pattern,title)
:#print('符合匹配條件:'+title)
return
true
else
:return
false
defdownload_ifo
(self,d)
:with
open
('d:/湖南郵政1/{}.html'
.format
(d['title'])
,'wb'
)as f:
content=requests.get(d[
'href'
],headers=headers)
.content
f.write(content)
defto_txt
(self,d)
:'''
檔案儲存為文字格式
'''with
open
('d:/湖南郵政1/明細.txt'
,'a'
,encoding=
'gbk'
)as f:
f.write(d[
'title']+
':'+d[
'href']+
'\n'
)def
to_csv
(self,d)
:'''
檔案儲存為csv格式
'''with
open
('d:/湖南郵政1/明細222.csv'
,'a'
,encoding=
'gbk'
)as f:
writer=csv.writer(f)
writer.writerow(
[d['title'
],d[
'href']]
)def
run(self,urls)
:for url in self.urls:
response=self.get_response(url)
ds=self.parse_response(response)
for d in ds:
if self.remove_duplicate(d)
:print
('符合匹配條件:'
+d['title'])
self.to_csv(d)
self.to_txt(d)
#else:
#print('無效資訊』:'+d['title'])
if __name__==
'__main__'
: youzheng=youzheng(urls)
youzheng.run(urls)
執行情況如何?
通過使用 net framework 免費提供的類,可以克服所有這些限制,這樣可以使您有效地編寫應用程式,以便使用者 管理員或其他支援人員都可以檢視日誌來診斷問題。返回頁首 net framework 提供了乙個強大的 eventlog 類,用它可以輕鬆地處理事件日誌。不過您不能直接從 vb6 訪問...
Sql Server 監控 Job 執行情況
由於最近資料庫的部分job總是不能十分健康的執行,而監控起來只能登陸db伺服器去檢視錯誤日誌,維護起來十分麻煩,並且job一般都是比較重要的,比如備份資料庫或者清理資料庫等等。這邊,經常由於清理job不能正常執行,造成資料庫空間在幾天內增加 2g 3g 左右,最後排查發現,是由於執行清理的sql語句...
linux 檢視埠執行情況
netstat命令各個引數說明如下 t 指明顯示tcp埠 u 指明顯示udp埠 l 僅顯示監聽套接字 所謂套接字就是使應用程式能夠讀寫與收發通訊協議 protocol 與資料的程式 p 顯示程序識別符號和程式名稱,每乙個套接字 埠都屬於乙個程式。n 不進行dns輪詢,顯示ip 可以加速操作 即可顯示...