本帖最后由 dreamrise 于 2018-10-8 17:50 编辑
整理里一遍楼主的代码,修改了两处可能的BUG后可以运行了。
[Python] 纯文本查看 复制代码
BUG1: 42行
title = description[i].h3.string
BUG2: 47行
img = 'https://bing.ioliu.cn' + pic_url[i].get('href')[:-9] + 'download'
原文中的[i]估计被编辑器吃掉了。
BUG3: 47行,这个BUG导致第10页开始无法下载,已修改。(难怪楼主建议不超过10页)
img = [color=#008080][b]'https://bing.ioliu.cn' [/b][/color]+ pic_url[i].get([color=#008080][b]'href'[/b][/color])[:-([color=#0000ff]8[/color]+[color=#000080]len[/color]([color=#000080]str[/color](k)))] + [color=#008080][b]'download'[/b][/color]
BUG4: 名称如果有/会下载不了,所以替换成_
[font=宋体][color=#000000][size=12px]name = name.replace('/','_').replace('\\','_')[/size][/color][/font]
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
import bs4,re,os,requests
session = requests.session()
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0"}
def fetch_content(url): # 获取任意网页内容,并用 beautifulsoup 格式化
req = session.get(url,headers = headers)
htmls = req.text
soup = bs4.BeautifulSoup(htmls,features = 'lxml')
return soup
print('此程序会从必应壁纸网站下载壁纸,并将壁纸保存在 D:\必应壁纸 目录下。')
s = input('键入任意键键开始,否则直接关闭程序:')
flag = False
if s != None:
flag = True
if flag:
if os.path.exists('D:\必应壁纸') == True: # 如果目录不存在则创建
print("目录已存在")
else:
os.mkdir('D:\必应壁纸')
print('目录创建成功')
k = 1
p = input('请输入要下载的页数(每页12张,建议不超过10页):')
child_flag = False
if p.isdigit() == True:
child_flag = True
if child_flag:
while k <= int(p):
url = 'https://bing.ioliu.cn/ranking?p=' + str(k) # p=6 即获取第六页的内容
soup = fetch_content(url)
pic_url = soup.body.find_all(class_='mark') # 获取图片子网址
description = soup.find_all(class_='description')
# 获取图片标题
for i in range(len(pic_url)):
# name = ''.join(re.findall('(\w*\S?\s*\w*\s*\S?\w+\S?\w*)\W{2}',description.h3.string))
title = description[i].h3.string
name = ''.join(re.findall('([\s\S]*?)\W{2}',title))
name = name.replace('/','_').replace('\\','_')
print('正在下载:\t', name) # 打印图片所属子页面以及图片标题
img = 'https://bing.ioliu.cn' + pic_url[i].get('href')[:-(8+len(str(k)))] + 'download'
img = session.get(img,proxies = {'http':'120.78.199.148'},headers = headers) # 下载
try:
if os.path.exists('D:/必应壁纸/' + name + '.jpg') == False:
with open('D:/必应壁纸/' + name + '.jpg','wb') as f:
f.write(img.content)
else:
pass
except:
pass
print('第%s页:%s张下载完成!'%(k,i + 1))
k += 1
print('%s页%s张全部下载完成!'%(int(p),int(p) * 12))
else:
print('请输入数字!')
exit('程序发生错误!')
else:
exit('程序发生错误!') |