You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.4 KiB
Python
57 lines
2.4 KiB
Python
#coding = utf-8
|
|
|
|
import requests
|
|
import parsel
|
|
import os
|
|
|
|
headers = {
|
|
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36"
|
|
}
|
|
|
|
def get_onepage(url):
|
|
response = requests.get(url=url,headers=headers)
|
|
selector = parsel.Selector(response.text)
|
|
lis = selector.css('.listdiv ul li')
|
|
for li in lis:
|
|
fail_title = li.css('.galleryli_title a ::text').get() # 文件名
|
|
href = li.css('.galleryli_title a ::attr(href)').get() # 详情页短地址
|
|
# global href_full
|
|
href_full = "https://www.fnvshen.com/" + href # 进入详情页的完整地址
|
|
print("正在准备下载" + href_full)
|
|
# print('正在下载相册:', fail_title)
|
|
|
|
# 创建相册文件夹
|
|
if not os.path.exists('img\\' + fail_title): # 如果该路径下没有该文件夹
|
|
os.mkdir('img\\' + fail_title)
|
|
|
|
try:
|
|
for page_url_num in range(1,101):
|
|
page_url = href_full + "{}.html".format(page_url_num)
|
|
response_page = requests.get(url=page_url,headers=headers)
|
|
selector_page = parsel.Selector(response_page.text)
|
|
imgs = selector_page.css(".gallery_wrapper ul#hgallery img") #获取详情页中单页的对象
|
|
for img in imgs:
|
|
img_name = img.css('img ::attr(alt)').get() #获得图片储存名称
|
|
img_href = img.css('img ::attr(src)').get() #图片下载地址
|
|
# print(img_name,img_href)
|
|
suffix = img_href.split('/')[-1] #获取图片下载后缀
|
|
response_page_1 = requests.get(url=img_href,headers=headers).content
|
|
with open(f'img\\{fail_title}\\{img_name}' + suffix, mode='wb') as f:
|
|
f.write(response_page_1)
|
|
print("保存完成:", img_name)
|
|
except:
|
|
print("该页已经是最后一页")
|
|
continue
|
|
|
|
|
|
# https://www.fnvshen.com/gallery/4.html
|
|
# https://www.fnvshen.com/gallery/3.html
|
|
|
|
for page in range(1,101):
|
|
print("正在下载第{}页".format(page))
|
|
url = "https://www.fnvshen.com/gallery/{}.html".format(page)
|
|
get_onepage(url)
|
|
|
|
|
|
|