★立即注册★

QQ登录

只需一步,快速开始

就爱江湖-应用库-技术分享资源网-Www.92Jh.Cn

查看: 301|回复: 0

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

[复制链接]
发表于 2019-8-22 10:39:35 | 显示全部楼层
话不多说先上结果



最后生成的照片墙

猫眼电影top100海报爬取和生成照片墙图片ID:1235



以下是源码


01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

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张海报图片
猫眼电影top100海报爬取和生成照片墙图片ID:5510



上一篇:【PHP】+恶搞图片生成器 在线生成图片【附源码 PSD分层 教程】
下一篇:利用爬虫将电影网站打包成一个APP【一】

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案,如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请在帖子回复【已解决】。

游客
回复
您需要登录后才可以回帖 登录 | ★立即注册★

免责声明
就爱江湖所发布的一切软件逆向解密分析文章及视频、破解补丁、注册机和注册信息,仅限用于学习和研究目的。不得将上述内容用于商业或者非法途径!否则,一切后果请用户自负!
本站信息来自互联网,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请购买注册正版软件,获得正版优质服务!
如有侵权请邮件或QQ与我们联系处理。

联系QQ:2710763
邮箱:2710763@qq.com

手机版|小黑屋| 就爱江湖资源分享网 |网站地图 |粤公网安备44180302000217号 | 粤ICP备2025451632号

GMT+8, 2025-9-9 18:44

Powered by Discuz!

© 2001-2020 Comsenz Inc.

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