暑假闲来无事,想看点凉快的图片,于是学了点爬虫,并有了如下结果。
此处有拼接图,为什么没了,那我也不知道哈哈哈哈哈哈。
1. 爬取图片
(不要恶意攻击网站,使用sleep方法,避免服务器宕机,最后一定要close)
# -*- coding: utf-8 -*-
# @Time : 2022/8/5 18:30
# @Author : luoyang
# @FileName: 02img.py
# @Software: PyCharm
import time
import requests
from bs4 import BeautifulSoup
domain = 'http://bizhi360.com'
index = domain + '/meinv'
endlist = ['', '/list_2.html', '/list_3.html']
j = 0
for i in range(3):
url = index+endlist[i]
resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)
page = BeautifulSoup(resp.text, 'html.parser')
resp.close()
findpage = page.find('ul').find_all('a')
# print(findpage)
for next_index in findpage:
child_url = domain + next_index.get('href')
child_resp = requests.get(child_url)
child_resp.encoding = 'utf-8'
child_page = BeautifulSoup(child_resp.text, 'html.parser')
downlink = child_page.find('figure').find('a').get('href')
# print(downlink)
child_resp.close()
img_resp = requests.get(downlink)
j += 1
# img_name = downlink.split('/')[-1]
with open('img/test{}.jpg'.format(j), mode='wb') as f:
f.write(img_resp.content)
img_resp.close()
print(j)
time.sleep(0.5)
2. 图片拼接
from PIL import Image
x, y = 1920, 1080
toImage = Image.new('RGB', (1920 * 8, 1080 * 8))
for i in range(8):
for j in range(8):
k = (i + 1) * (j + 1)
with Image.open('test{}.jpg'.format(k)) as f:
toImage.paste(f, (i*x, y*j))
toImage.show()
toImage.save('toImage.png')
参考:
哔哩哔哩大学(自行搜索爬虫教程)