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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5956|回复: 13
收起左侧

[Python 转载] 猫眼电影top100海报爬取和生成照片墙

  [复制链接]
天空宫阙 发表于 2019-8-20 17:06


话不多说先上结果



最后生成的照片墙

1.png



以下是源码

[Python] 纯文本查看 复制代码
import requests
from bs4 import BeautifulSoup
import re
import time
import os
import math
import pickle
from PIL import Image

#存图片的路径
path = 'result/'

headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}

#请求返回网页源码
def get_html(url):
    response = requests.get(url,headers=headers)
    if response.status_code ==200:
        return response.text
    else:
        print('请求网页失败')

#解析网页源码得到影片title和海报src
def parse(html):
    soup = BeautifulSoup(html,'lxml')
    dl = soup.select('#app > div > div > div.main > dl')[0]
    dds = dl('dd')
    for dd in dds:
        title = dd.find('a')['title']
        star = dd.select('.star')[0].string.strip()
        img = dd.select('img.board-img')[0]['data-src']
        yield {
            'title':title,
            'star':star,
            'img':img
        }

#路径检测
def make_dir(path):
    if not os.path.exists(path):
        os.makedirs(path)

#下载影片海报至result文件夹
def downlord_img(title,img_src):
    make_dir(path)
    match = re.search('(https.*?jpg)',img_src,re.S)
    if match:
        src = match.group(1)
        response = requests.get(src,headers=headers)
        if response.status_code ==200:
            with open(os.path.join(path,title+'.jpg'),'ab') as f:
                f.write(response.content)
                f.close()

#这段代码作者@Charles
#指定文件夹内图片生成图片墙
def makePicturesWall(picdir):
    picslist = os.listdir(picdir)
    num_pics = len(picslist)
    size = 128
    line_numpics = int(math.sqrt(num_pics))
    picwall = Image.new('RGBA', (line_numpics*size, line_numpics*size))
    x = 0
    y = 0
    for pic in picslist:
        img = Image.open(os.path.join(picdir, pic))
        img = img.resize((size, size), Image.ANTIALIAS)
        picwall.paste(img, (x*size, y*size))
        x += 1
        if x == line_numpics:
            x = 0
            y += 1
    print('图片墙制作成功!')
    picwall.save("picwall.png")

def main():
    count = 0
    for i in range(0,10):
        print('准备下载第{}页'.format(str(i+1)))
        url = 'https://maoyan.com/board/4?offset={}'.format(str(i*10))
        html = get_html(url)
        data = parse(html)
        for each in data:
            count+=1
            downlord_img(each['title'],each['img'])
            print('下载{}海报成功'.format(each['title']))
        print('准备睡眠3s')
        time.sleep(3)
    makePicturesWall(path)

if __name__ == '__main__':
    main()


其他想说的

1.新手上路请多指教

2.使用方法用pip3安装相关库后直接run即可

下载的100张海报图片

下载的100张海报图片

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
苏紫方璇 + 2 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

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

 楼主| 天空宫阙 发表于 2019-8-21 07:07
coradong1985 发表于 2019-8-20 20:48
敢把图片弄个包不,以后做NAS影库能用上

你的意思是就要这100张图片?还是说要下100张图片的程序封装成的exe文件?
100张图片范围太小了怕是帮不上你,下图片的程序也没有通用性封装也没人用
qq63 发表于 2019-10-10 13:32
  File "1.py", line 10
SyntaxError: Non-ASCII character '\xb4' in file 1.py on line 10, but no encoding
declared; see http://www.python.org/peps/pep-0263.html for details

这个怎么处理??
339779394 发表于 2019-8-20 17:27
 楼主| 天空宫阙 发表于 2019-8-20 17:35
339779394 发表于 2019-8-20 17:27
这个方法有很多种啊

应该是有很多方法的,Python的话用pillow这个库比较方便。
醉看小风月 发表于 2019-8-20 18:11
纯小白不会用
头像被屏蔽
coradong1985 发表于 2019-8-20 20:48
提示: 作者被禁止或删除 内容自动屏蔽
头像被屏蔽
coradong1985 发表于 2019-8-21 14:03
提示: 作者被禁止或删除 内容自动屏蔽
 楼主| 天空宫阙 发表于 2019-8-21 16:46
coradong1985 发表于 2019-8-21 14:03
我的意思其实是NAS影库是什么东西?

简单来说就是私有云,贫苦人民用不起
想了解更多自行百度,知乎
回首天涯 发表于 2019-8-26 12:44
coradong1985 发表于 2019-8-21 14:03
我的意思其实是NAS影库是什么东西?

好像是要付费的
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-25 17:23

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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