37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
# @Time : 2022-7-24 0024 11:18
|
|
# @Author : Qing
|
|
# @Email : derighoid@gmail.com
|
|
# @File : Picture _Crawling.py
|
|
# @Software: PyCharm
|
|
|
|
# 导入库
|
|
import os
|
|
import requests
|
|
|
|
|
|
# path = '(?<=<img(?:.*)\s+src=")[^"]+(\.png|jpg|jpeg)'
|
|
# 定义函数
|
|
def pictureCrawl(url):
|
|
# 文件保存地址
|
|
root = "E://桌面//Python_Picture_Analysis//data//"
|
|
# 文件的保存地址以及格式
|
|
path = root + url.split('/')[-3] + '.png'
|
|
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 fp:
|
|
fp.write(r.content)
|
|
print("OK")
|
|
else:
|
|
print("File already exists")
|
|
except:
|
|
print("Crawl successful")
|
|
|
|
|
|
# url = "https://tse2-mm.cn.bing.net/th/id/OIP-C.fT7uKiT7V7YO2PPINFeOdQHaJ4?w=186&h=248&c=7&r=0&o=5&dpr=1.25&pid=1.7"
|
|
# pictureCrawl(url)
|