吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 15742|回复: 25
收起左侧

[Python 转载] python下载高清壁纸

  [复制链接]
fly-with-fire 发表于 2018-9-9 20:33
若有违规行为,请管理员帮忙删除,谢谢!

壁纸网址:https://bing.ioliu.cn/ranking,爬取的是排行榜上的壁纸

源码:

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.h3.string
                name = ''.join(re.findall('([\s\S]*?)\W{2}',title))

                print('正在下载:\t', name)                                                       # 打印图片所属子页面以及图片标题

                img = 'https://bing.ioliu.cn' + pic_url.get('href')[:-9] + '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('程序发生错误!')

程序运行如图:
001.png

002.png
(我也不知道为什么这么不清晰)

这是一些下载好的图片:
被萤火虫照亮的小树林,日本四国岛.jpg
东库特尼的埃尔克河,加拿大不列颠哥伦比亚省.jpg

&amp;#197;lvundeidet村附近的Innerdalsvatna湖,挪威.jpg

最后用pyinstaller 打包成了一个可执行程序(在同学电脑上测试可以正常运行),这个程序国产软件会报毒(例如腾讯安全管家),添加信任就好,不过我的eset就没有报。再次证明国产杀毒软件就是垃圾!!!!


eset 扫描结果:

003.png

004.png

bizhi.rar (1.37 KB, 下载次数: 202)
程序下载地址:https://www.lanzous.com/i1u8cij

003.png

免费评分

参与人数 5吾爱币 +6 热心值 +4 收起 理由
Vikiya丶TTT + 1 谢谢@Thanks!
风轻然雨朦胧 + 1 + 1 热心回复!
adlink29 + 2 + 1 谢谢@Thanks!
顾月时550 + 1 + 1 谢谢@Thanks!
拔剑不知谁为雄 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

dreamrise 发表于 2018-10-8 15:33
本帖最后由 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('程序发生错误!')

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
niebaohua + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| fly-with-fire 发表于 2018-9-9 20:35
程序如果不能正常运行就。。。就别找我了,我也是刚接触
dreamrise 发表于 2018-10-8 14:09
nj19 发表于 2018-10-8 14:37
程序可以运行,
流星的孤单 发表于 2018-10-9 08:30
路过,顶一个
pzypdl 发表于 2018-10-9 11:09
第25行代码报错的,把22行和25行的反斜杠符号\改成正斜杠符号/
MEcrush 发表于 2018-10-12 22:00
话说怎么用的 我在win10 上只能打开软件,但是无任何反应
雨足 发表于 2018-10-30 10:56
fly-with-fire 发表于 2018-9-9 20:35
程序如果不能正常运行就。。。就别找我了,我也是刚接触

请楼主根据4楼的建议修改下,不然10页以后的壁纸无法下载。。。。
还有就是能不能指定下载某页的壁纸,不然每次都要全部重新下载,谢谢!
雨足 发表于 2018-10-30 11:07
dreamrise 发表于 2018-10-8 15:33
整理里一遍楼主的代码,修改了两处可能的BUG后可以运行了。
[mw_shl_code=python,true]
BUG1: 42行

请问能不能帮忙打包一个EXE可执行文件,谢谢!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 18:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表