Navigation

    GitHub中文社区
    • GitHub排行榜

    论坛

    • Login
    • Categories
    • Recent
    • Tags
    • Popular

    我是小菜鸡一个 求大神改一下错误,感谢

    技术交流
    2
    2
    318
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • songyuanyuan666
      songyuanyuan666 last edited by

      import requests
      import os
      from bs4 import BeautifulSoup

      指定要爬取的网页链接

      url = "https://example.com/"

      发送GET请求获取网页内容

      response = requests.get(url)

      解析网页内容

      soup = BeautifulSoup(response.content, "html.parser")

      获取所有图片链接

      img_tags = soup.find_all("img")

      指定本地保存路径

      save_path = "/path/to/save/directory/"

      如果保存路径不存在则创建

      if not os.path.exists(save_path):
      os.makedirs(save_path)

      循环下载jpg后缀的图片

      for img in img_tags:
      # 获取图片链接
      img_url = img.get("src")

      # 判断链接是否以jpg结尾
      if img_url.endswith(".jpg"):
          # 发送GET请求获取图片数据
          response = requests.get(img_url)
      
          # 获取图片的文件名
          img_name = os.path.basename(img_url)
      
          # 拼接本地保存路径和图片文件名
          img_path = os.path.join(save_path, img_name)
      
          # 保存图片到本地文件
          with open(img_path, "wb") as f:
              f.write(response.content)
      
          # 输出保存成功信息
          print("图片已保存到:", img_path)
      1 Reply Last reply Reply Quote 0
      • loveme199
        loveme199 last edited by

        哪儿报错,你得先说清楚啊。

        1 Reply Last reply Reply Quote 0
        • First post
          Last post