好久不見呀,各位,[/壞笑]
自從上次發布文章已經過去了許久,之前承諾過的2.0版本我就現在嘛出來吧,(畢竟,評論區都已經開始催了,拖不了了...)
emm...具體的網頁鏈接我就不寫在正文了,我會放在代碼區的注釋部分,
閑話不多說,下面就是本次更新的代碼:
# 目標網址:https://www.xiurenb.com
# 匯入庫
import time, os, requests
from lxml import etree
from urllib import parse
# 定義請求頭
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'
}
# 格式化串列
img_list = []
url_list = []
page_list = []
# 編碼輸入資料
human_unencode = input('Enter the human_name:')
human_encode = parse.quote(human_unencode)
# 編碼后索引url
url_human = 'https://www.xiurenb.com/plus/search/index.asp?keyword=' + str(human_encode) + '&searchtype=title'
# 獲取指定人物寫真集串列頁數
res_first = requests.get(url=url_human, headers=headers)
tree_first = etree.HTML(res_first.text)
Num_first = len(tree_first.xpath('/html/body/div[3]/div[1]/div/div/ul/div[3]/div/div[2]/a'))
print(f'Page_total:{Num_first})
# 獲取指定頁數的每個寫真集的url并寫入串列
i = input('Enter the PageNumber:)
print(f'Getting the page-{i}...')
res_human = requests.get(url_human + '&p=' + str(i))
tree_human = etree.HTML(res_human.text)
jihe_human = tree_human.xpath('/html/body/div[3]/div[1]/div/div/ul/div[3]/div/div[1]/div/div[1]/h2/a/@href')
for page in jihe_human:
page_list.append(page)
time.sleep(2)
# 獲取每個寫真集的全部圖片
for Page_Num in page_list:
url = 'https://www.xiurenb.com' + str(Page_Num)
Num_res = requests.get(url=url, headers=headers)
Num_tree = etree.HTML(Num_res.text)
Num = len(Num_tree.xpath('/html/body/div[3]/div/div/div[4]/div/div/a'))
url_list.append(url)
for i in range(1, int(Num) - 2):
url_other = url[:-5] + '_' + str(i) +'.html'
url_list.append(url_other)
# 獲取所有圖片url
for url_img in url_list:
res = requests.get(url=url_img, headers=headers)
tree = etree.HTML(res.text)
img_src = https://www.cnblogs.com/moxing-wanqian/p/tree.xpath('/html/body/div[3]/div/div/div[5]/p/img/@src')
for img in img_src:
img_list.append(img)
time.sleep(0.5)
# 創建保存目錄
res = requests.get(url=url_list[0], headers=headers)
res.encoding = 'utf-8'
tree = etree.HTML(res.text)
path_name = tree.xpath('/html/body/div[3]/div/div/div[1]/h1//text()')[0][11:]
print(path_name)
if not os.path.exists(f'C:/Users/liu/Pictures/{human_unencode}'):
os.mkdir(f'C:/Users/liu/Pictures/{human_unencode}')
the_path_name = f'C:/Users/liu/Pictures/{human_unencode}/' + path_name
if not os.path.exists(the_path_name):
os.mkdir(the_path_name)
# 保存圖片資料
num = 0
for j in img_list:
img_url = 'https://www.xiurenb.com' + j
img_data = https://www.cnblogs.com/moxing-wanqian/p/requests.get(url=img_url, headers=headers).content
img_name = img_url.split('/')[-1]
finish_num = str(num) + '/' + str(len(img_list))
with open(f'C:/Users/liu/Pictures/{human_unencode}/' + path_name + '/' + img_name, 'wb') as f:
print(f'Downloading the img:{img_name}/{finish_num}')
f.write(img_data)
f.close()
num += 1
time.sleep(0.5)
# 再次格式化串列
img_list = []
url_list = []
else:
print('gone>>>')
# 再次格式化串列
img_list = []
url_list = []
# 輸出結束提示
print('Finished!')
這次代碼比較長,我就不一一解釋了,這里需要注意的是,記得把保存路徑換成自己的,畢竟用戶名不同,
這個版本就是通過人名搜索寫真集,比如:唐安琪,運行代碼時輸入想要搜索的內容,中間再輸入想要下載的頁數就可以了,
如果有什么其他問題的話,可以評論區問我,
當然,如果我解決不了的話我會去補課的[/痛哭],畢竟我學python也沒多久...
本文來自博客園,作者:魔性萬千,轉載請注明原文鏈接:https://www.cnblogs.com/moxing-wanqian/p/moxingwanqian_1.html
同時,本文章也同時上傳至csdn,作者:混沌神
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501593.html
標籤:Python
上一篇:《笨辦法學Python3 》入坑必備,并不是真笨學!!!
下一篇:如何運行python程式
