使用bs4爬取图片并进行图片拼接

暑假闲来无事,想看点凉快的图片,于是学了点爬虫,并有了如下结果。

此处有拼接图,为什么没了,那我也不知道哈哈哈哈哈哈。

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')

参考:

python实现图片拼接 - 腾讯云开发者社区-腾讯云

哔哩哔哩大学(自行搜索爬虫教程)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值