1. 程式人生 > >我的第一篇文章,非複製貼上,記錄我的部落格之路

我的第一篇文章,非複製貼上,記錄我的部落格之路

查詢檔案,儲存檔案,正則獲取

匯入

import urllib.request,ssl,re,os

儲存為HTML檔案

def writeFileBytes(htmlBytes):
with open(r’D:\Users\zoe\PycharmProjects\tcp程式設計\html檔案\iphon.html’,’wb’) as f:
f.write(htmlBytes)

儲存為txt檔案(路徑html檔案請忽略)

def writeFileStr(htmlBytes):
with open(r’D:\Users\zoe\PycharmProjects\tcp程式設計\html檔案\iphon2.txt’,’w’) as f:
f.write(htmlBytes)
#請求包括資訊頭,ssl,網址
def getheaders(url):
headers = {“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; WOW64; rv:61.0) Gecko/20100101 Firefox/61.0”}
req = urllib.request.Request(url,headers=headers)
context = ssl._create_unverified_context()
respones = urllib.request.urlopen(req,context=context)
return respones.read()
#調取儲存檔案方法,最簡單的正則,儲存檔案str方式,(直接返回列表檔案比較方便)
def iPhone(url,path):
htmlBytes = getheaders(url)
#HtmlBytes = writeFileBytes(htmlBytes)
#HtmlStr = writeFileStr(htmlBytes.decode(‘utf-8’))
r = r’1\d{10}’
rep = re.compile(r)
iphon = rep.findall(htmlBytes.decode(‘utf-8’))
with open(path,’w’)as f:
f.write(str(iphon))

iphone = iPhone(url,path)
print(iphone)