zoukankan      html  css  js  c++  java
  • 日报3.1

    今天爬取中药的图片,最初是采用直接爬取整个网页,然后解析爬取网页中的图片,由于网页中存在很多其他图片并且不好命名,所以用之前爬下来的数据才能够数据库中读取,根据图片的具体地址爬取图片

    #图片爬取全代码
    import requests
    import os
    import 数据库连接
    
    
    #定义一个函数参数为图片的地址,以及文件名
    def picture (url ,str1):
        root = 'F:\大学\毕业设计\中药图片\'    #图片的存储地址
        path = root + str1 + '.jpg'        #图片的地址以及名称
        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('爬取失败')
    #url='http://www.zhongyoo.com/uploads/allimg/2011/1-201119163434137-lp.jpg'
    
    #path=root+url.split('/')[-1]#被加数表示的是图片路径中图片的名字加后缀
    #path=root+ '苦玄参' + '.jpg'
    
    if __name__ == '__main__':
        dbt = 数据库连接.jdbc_connect("localhost", "root", "gy1212", "zhongyao")
        sql = "select * from 中药 "
        srcs = dbt.select(sql)
        for row in srcs:
            str1 = row[0]
            src = row[2]
            picture(src,str1)
  • 相关阅读:
    win10新特性,ubuntu子系统(安装及配置)
    excel计算后列填充
    word中利用宏替换标点标点全角与半角
    CFD-post的奇技淫巧
    mfix模拟流化床燃烧帮助收敛的方法
    cygwin下配置alias
    endnote 使用方法
    win8系统换win7系统
    python中的函数以及递归
    01python算法--算法和数据结构是什么鬼?
  • 原文地址:https://www.cnblogs.com/1gaoyu/p/14469765.html
Copyright © 2011-2022 走看看