Python爬虫:7_BeautifulSoup4图片爬取

本文介绍了如何使用Python的BeautifulSoup4库进行图片爬取,包括安装步骤、具体代码实现、爬取效果展示以及注意事项。特别提示,为了优化Pycharm性能,需将img文件夹设置为Excluded,避免索引导致的卡顿。
摘要由CSDN通过智能技术生成

安装

pip install bs4

代码

import requests
from bs4 import BeautifulSoup
import time

url = 'https://umei.cc/bizhitupian/weimeibizhi/'
domain = 'https://umei.cc'

resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)

# 把网页源代码传给bs
page = BeautifulSoup(resp.text, 'html.parser')
a_list = page.find('div', class_='TypeList').find_all('a')
# print(a_list)
for a in a_list:
    # 使用get可以直接拿到属性值
    href = domain + a.get('href')
    child_resp = requests.get(href)
    child_resp.encoding = 'utf-8'
    child_content = child_resp.text
    # 从子页面中拿图片的下载路径
    child_page = BeautifulSoup(child_content, 'html.parser')
    p = child_page.find('p', align='center')
    # print(p)
    img = p.find('img')
    src = img.get('src')
    # 下载图片
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值