鱼C论坛

 找回密码
 立即注册
查看: 934|回复: 1

求大神指点一下爬虫代码

[复制链接]
发表于 2019-7-23 15:56:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小白一只,比着网上的教程敲代码...但是遇到一个爬图片的问题,如下的代码,无论怎么运行都是爬取失败,请大神指点一下问题出在哪里???谢谢了

import requests
import os
url ="https://img01.sogoucdn.com/net/a/04/link?url=https%3A%2F%2Fimg04.sogoucdn.com%2Fapp%2Fa%2F100520093%2Fca86e620b9e623ff-d72d635343d5bade-e65325a00e54354fe47fc2704d44165f.jpg&appid=122"
root = "D;//pics//"
path = root + url.split('/')[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        r = requests.get(url)
        with open(path,'wb') as f:
            f.write(r.content)
            f.close()
            print("文件保存成功")
    else:
            print("文件已存在")
except:
    print("爬取失败")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-23 23:30:57 | 显示全部楼层
写入有问题,注意路径,你就保存一个图片,直接保存就可以
  1. url = "https://img01.sogoucdn.com/net/a/04/link?url=https%3A%2F%2Fimg04.sogoucdn.com%2Fapp%2Fa%2F100520093%2Fca86e620b9e623ff-d72d635343d5bade-e65325a00e54354fe47fc2704d44165f.jpg&appid=122"
  2. root = r"C:\Users\Administrator\Desktop\0.jpg"
  3. r = requests.get(url)
  4. with open(root, 'wb') as f:
  5.     f.write(r.content)
  6.     f.close()
  7.     print("文件保存成功")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-17 03:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表