600个,分享不错的资源

600个,分享不错的资源 最新 图1张

文字 | 配图 | 排版 | ?老Y

官网:www.youquhome.cn


hello大家好,这里是每天爆肝更新的老Y工作室。


事情是这样的,最近想给女神找手机壁纸,刚好昨天老Y在逛知乎的时候发现了一个问题,如下:


600个,分享不错的资源 最新 图2张


老Y看了下,默认排序的前2个的回答分享了不少超赞的手机壁纸,看他们的点赞数就知道壁纸质量了,点赞数分别是16.7万、10.1万。而且分享的手机壁纸都是没开水印的。


于是想着把前2个高赞的回答里的壁纸都给下载下来然后分享给大家,想着用python下载可能要方便点,在网上找了一段代码,稍微改了下。


实现了分文件夹下载回答者的图片,你也可以自己修改下载前几位回答者的图片,代码也没用多线程,就是一个个保存。


600个,分享不错的资源 最新 图3张


600个,分享不错的资源 最新 图4张

……


壁纸一个大概700多个,老Y已经打包供大家下载,下载地址如下:

https://ilaoygzs.lanzoui.com/iRumnugncwd


另外,老Y把代码也放在下面,如果想下载其他的图片也可以用,大家也可以用图片下载的浏览器插件ImageAssistant,可以看下这篇文章(推荐4个非常好用的插件)。


代码还没来得及好好调试,下载前两个回答暂时没有bug,感兴趣的同学自己完善吧。


import urllib.request
import urllib
import os
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup

url = 'https://www.zhihu.com/question/329062584'#问题网址
def get_soup(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    soup = BeautifulSoup(html, 'html.parser')
    return soup
def get_question_title(soup):
    title=soup.find_all('h1', class_='QuestionHeader-title')[0].text
    print('问题:{}'.format(title))
def get_img(soup):
    for auther in range(2):#下载前两个回答
        imglist = []
        for item in soup.find_all('div', class_='RichContent-inner')[auther]:
            figure=item.find_all('figure')
            for t0 in figure:
                t1=t0.find_all('img')
                for t2 in t1:
                    t3=t2.get('src')
                    imglist.append(t3)
      # 表示在整个网页中过滤出所有图片的地址,放在imglist中
        auther = soup.find_all('div', class_='AuthorInfo-content')[auther]
        autherinfo = auther.find_all('a')[0].text
        print('开始下载回答者 {} 的图片'.format(autherinfo))
        path = autherinfo
        #print(imglist)
        paths=''
      # 将图片保存到回答者名字的文件夹中,如果没有image文件夹则创建
        if not os.path.isdir(path):
            os.makedirs(path)
            paths = path + '\\'  # 保存在回答者路径下
        else:
            paths=autherinfo+'\\'
        idx = 1
        for imgurl in tqdm(imglist):
            a = imgurl.startswith('http')
            if (a):
                try:
                    urllib.request.urlretrieve(imgurl,'{0}{1}.png'.format(paths,idx))  # 打开imglist中保存的图片网址,并下载图片保存在本地,format格式化字符串
                    idx = idx + 1
                except:
                    pass
if __name__ == '__main__':
    soup=get_soup(url)
    get_question_title(soup)
    get_img(soup)


600个,分享不错的资源 最新 图5张


– End –

文章转载自微信公众号:老Y工作室