使用python下载王者荣耀官网上的图片 (爬虫)

路漫漫其修远兮,吾将上下而求索

技术就要分享,才能更快促进技术进一步提升。如果您有任何意见和想法欢迎找我一起聊聊。

代码

使用python下载王者荣耀官网上的图片

import requests
from bs4 import BeautifulSoup

h = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"
}
resp = requests.get('https://www.umeitu.com/meinvtupian/meinvxiezhen/',headers=h)
resp.encoding='utf-8'
bs = BeautifulSoup(resp.text,'html.parser')
list = bs.find("div",attrs={"class":"NewPages"}).find_all("a")
for a in list:
    href = 'https://www.umeitu.com'+a.get("href")
    print(href,'\n')
    resp1 = requests.get(href)
    resp1.encoding = 'utf-8'
    child_page = BeautifulSoup(resp1.text, "html.parser")
    list1 = child_page.find("div",attrs={"class":"TypeList"}).find_all("a",attrs={"class":"TypeBigPics"})
    n = 1
    for a1 in list1:
        href1 = 'https://www.umeitu.com' + a1.get("href")
        #print(href1) #新的网页
      # 进入新的链接
        resp11 = requests.get(href1)
        resp11.encoding = 'utf-8'
        child_page1 = BeautifulSoup(resp11.text, "html.parser")
     # 找到图片的真实路径
        src = child_page1.find("div", attrs={"class": "ImageBody"}).find("img").get("src")
        print(src)  # 图片路径

        #发送请求将图片保存到本地
        #创建文件
        f = open(r"E:\图片\图片_%d.jpg" %n,mode="wb")#wb表示写入内容是非文本
        f.write(requests.get(src).content)#向外拿出图片的数据,不是文本
        n += 1 #n自增
        print("下载好了一张图片")
resp.close()

注意 :使用之前在E盘下面创建一个 图片 的文件夹

再运行此代码,就会将文件下载到这个文件夹里面

希望对您有所帮助,谢谢观看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值