猫眼电影top100海报爬取和生成照片墙
话不多说先上结果
最后生成的照片墙
以下是源码
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')
dds = dl('dd')
for dd in dds:
title = dd.find('a')['title']
star = dd.select('.star').string.strip()
img = dd.select('img.board-img')['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即可 coradong1985 发表于 2019-8-20 20:48
敢把图片弄个包不,以后做NAS影库能用上
你的意思是就要这100张图片?还是说要下100张图片的程序封装成的exe文件?
100张图片范围太小了怕是帮不上你,下图片的程序也没有通用性封装也没人用 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
这个方法有很多种啊
应该是有很多方法的,Python的话用pillow这个库比较方便。 纯小白不会用 coradong1985 发表于 2019-8-21 14:03
我的意思其实是NAS影库是什么东西?
简单来说就是私有云,贫苦人民用不起
想了解更多自行百度,知乎 coradong1985 发表于 2019-8-21 14:03
我的意思其实是NAS影库是什么东西?
好像是要付费的
页:
[1]
2