Python爬虫 学习Bs4 爬取高清图片

from bs4 import BeautifulSoup
import requests
import time

url = 'https://www.umeituku.com/bizhitupian/diannaobizhi/'
resp = requests.get(url)
resp.encoding = 'utf-8'
# print(resp.text)

main_page = BeautifulSoup(resp.text, 'html.parser')
alist = main_page.find('div', class_='TypeList').find_all('a')
# print(alist)

for a in alist:
    href = a.get('href')
    # 拿到子页面的源代码
    chile_page = requests.get(href)
    chile_page.encoding = 'utf-8'
    chile_page_html = BeautifulSoup(chile_page.text, 'html.parser')
    # 定位到div class=ImageBody
    div_li = chile_page_html.find('div', class_='ImageBody').find_all('img')
    for img in div_li:
        img_src = img.get('src')
        # print(img_src)
        # 下载图片
        img_resp = requests.get(img_src)
        img_resp.content  # 这里拿到的是字节
        image_name = img_src.split('/')[-1]
        with open(image_name, 'wb') as f:
            f.write(img_resp.content)  # 写入图片
        time.sleep(1)  # 增加容错
        print("success", image_name)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值