BS模块之抓取优美图库

#1.拿到主页面的源代码,然后提取到子页面的链接地址,href
#2.通过href拿到子页面的内容,从子页面中找到图片的下载地址 img-> src
#3.下载图片
import requests
from bs4 import BeautifulSoup
import time

url = "https://umei.cc/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = 'utf-8'
resp.close()
# print(resp.text)
#把源代码交给bs
main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div", class_="TypeList").find_all("a")
# print(alist)
for a in alist:
    href = "https://umei.cc/" + a.get('href')    #通过get就可以直接拿到属性的值
    # print(href)
    #拿到子页面的源代码
    child_page_resp = requests.get(href)
    child_page_resp.encoding = 'utf-8'
    child_page_text = child_page_resp.text
    # print(child_page_text)
    #从子页面中拿到图片的下载路径
    child_page = BeautifulSoup(child_page_text, "html.parser")
    p = child_page.find("p",align="center")
    img = p.find("img")
    src = img.get("src")
    #下载图片
    img_resp = requests.get(src)
    # img_resp,content  #这里拿到的是字节
    img_name = src.split("/")[-1]   # 拿到url中的最后一个/以后的内容
    with open("png/"+img_name, mode="wb") as f:
        f.write(img_resp.content)   #图片内容写入文件

    print("over!!!",img_name)
    time.sleep(1)

print("allover!!!")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值