記錄學(xué)習(xí)Python歷程
簡(jiǎn)單了解語(yǔ)法規(guī)則桩蓉,嘗試?yán)谜齽t采集網(wǎng)站圖片淋纲,結(jié)果還算理想,不過(guò)只寫了手動(dòng)查詢的触机,自動(dòng)采集加倆循環(huán)搞定帚戳,只是語(yǔ)法儲(chǔ)備不足,還是有很多冗余代碼儡首,繼續(xù)學(xué)習(xí)F巍J呖琛对供!繼續(xù)擼起袖子加油干,奧利給!6焖琛京景!
import urllib.request
import re
import urllib
#函數(shù)返回值:cod 1為失敗 0為成功
#函數(shù)返回值格式
def return_msg(cod,msg,data):
res = {}
res['cod'] = cod
res['msg'] = msg
res['data'] = data
return res
#獲取網(wǎng)頁(yè)源碼
def get_html(url):
page = urllib.request.urlopen(url)
html_a = page.read()
return html_a.decode('utf-8')
#正則提取數(shù)組操作
def re_ac(html,reg):
re_res = re.compile(reg)
arr = re_res.findall(html)
res = return_msg(1,"匹配失敗",'')
if len(arr) !=0:
res = return_msg(0, "匹配成功", arr)
return res
#正則提取數(shù)組操作 允許換行
def re_ac_tall(html,reg):
re_res = re.compile(reg,re.DOTALL)
arr = re_res.findall(html)
res = return_msg(1,"匹配失敗",'')
if len(arr) != 0:
res = return_msg(0, "匹配成功", arr)
return res
#獲取子頁(yè)面地址
def get_son_url(html):
urllist = re_ac(html,r'<a href="(.*?)" class="img"')
return urllist
#獲取子頁(yè)面所有圖片
def get_son_img(html):
imglist =re_ac(html,r'<img class="lazy" src="(.*?)"')
return imglist
#獲取下一頁(yè)鏈接
def get_nexturl(html):
urls =re_ac(html,r'<div class="pageNum wp">(.*?)</div>')
res = urls
if urls['cod'] == 0:
nexturl = urls['data']
if nexturl != '':
if '下一頁(yè)' in nexturl:
url = re_ac(nexturl,r '<a href="(.*?)">')
res = url
if url['cod'] == 1:
res = return_msg(2, "沒(méi)有下一頁(yè)了", '')
else:
res = return_msg(2, "沒(méi)有下一頁(yè)了", '')
else:
res = return_msg(1, "數(shù)據(jù)匹配失敗", '')
return res
# 獲取分類
def get_class(html):
info = re_ac_tall(html,r'<div class="subnav-l z">(.*?)</div>')
res = info
if info['cod'] == 0:
infos = re_ac(info['data'][0], r'href="(.*?)" >(.*?)<')
if infos['cod'] == 0:
res = infos
else:
res = return_msg(1, "數(shù)據(jù)匹配失敗", '')
else:
res = return_msg(1, "數(shù)據(jù)匹配失敗", '')
return res
host = "https://www."+"wo"+"yao"+"ge"+"xing"+".com"
#讀取圖片
html_b = get_html(host+"/touxiang/")
if html_b != '':
n_class = get_class(html_b)
n_class_data = n_class['data']
if n_class['cod'] == 0:
while True:
i = 0
print("PS:請(qǐng)根據(jù)序號(hào)查詢分類圖片")
for v in n_class['data']:
print(str(i) + '---' + str(v[1]))
i += 1
index = input("請(qǐng)輸入查詢序號(hào):")
if index == '-1':
print("已退出查詢")
break
elif int(index) > len(n_class['data']) or int(index) < 0:
print("請(qǐng)輸入正確的序號(hào)")
else:
page = input("請(qǐng)輸入查詢頁(yè)碼:")
while int(page) <= 0:
print("請(qǐng)輸入大于等于1的頁(yè)碼")
page = input("請(qǐng)輸入查詢頁(yè)碼:")
print(host + n_class['data'][int(index)][0] + "index_" + page + ".html")
class_url = host + n_class['data'][int(index)][0]
if int(page) > 1:
class_url = host + n_class['data'][int(index)][0] + "/index_" + page + ".html"
html_c = get_html(class_url)
urllist = get_son_url(html_c)
if urllist['cod'] == 0:
for url in urllist['data']:
html = get_html(host + url)
imgres = get_son_img(html)
if imgres['cod'] == 0:
print(imgres['data'])
else:
break
print(imgres)
else:
break
print(urllist)
else:
print("分類獲取失敗")
else:
print("數(shù)據(jù)查詢失敗")