一、获取网址,解析网址

(1)、for循环获取不同页的数据

(2)、获取a标签中的网址(原因:外面图片分辨率太小,进入图片对应的网址寻找分辨率大点的图片)
1# url-网址 https://www.moyublog.com/95-2-0-0.html 2for i in range(1): 3 str_value = str(i) 4 # 页数 5 url = "https://www.moyublog.com/95-2-0-" + str_value + ".html" 6 # 浏览器类型-搜狗 7 Search_engine = {"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"} 8 # 发送请求,获取网址HTML,转为text 9 Type_conversion = requests.get(url=url, headers=Search_engine, timeout=None).text 10 # 定义BeautifulSoup,解析网址HTML 11 bs = BeautifulSoup(Type_conversion, 'html.parser') 12 # 获取指定div 13 scope_div = bs.find('div', attrs={'class': 'slist'}) 14 # 获取指定div中的所有a标签 15 scope_a = scope_div.findAll('a') 16 # print(scope_a)
(3)、进入对应图片的网址,解析对应的数据

1# 循环获取指定div中的所有a标签 2for int_A in scope_a: 3 # 清除不需要的a标签 4 if len(int_A) % 2 == 0: 5 # 获取定的a标签中的链接 6 get_href = int_A.get("href") 7 # print(get_href) 8 # 新的url-网址 9 url_A = get_href 10 # 发送请求,获取网址HTML,转为text,发送请求,获取网址HTML,转为text, 11 Type_conversion_A = requests.get(url=url_A, headers=Search_engine).text.encode('iso-8859-1').decode( 12 'utf-8') 13 # 定义新的BeautifulSoup,解析网址HTML 14 bs_A = BeautifulSoup(Type_conversion_A, 'html.parser') 15 # 定义新的网址中的div标签 16 A_div = bs_A.find('div', attrs={'class': 'photo-pic'}) 17 # 获取新定义div中的所有img标签 18 A_img = A_div.findAll('img') 19 # print(A_img)
二、图片下载并且保存
(1)、判断并且创建文件夹
1folder = "image" 2if not os.path.exists(folder): 3 print("文件不存在,已创建!") 4 os.mkdir(folder) 5else: 6 print("开始下载图片")
(2)、打开文件夹下载图片并且命名
1# 获取img标签中的src属性中的内容 2get_a_src = a_img.get("src") 3# 获取img标签中的title属性中的内容 4get_a_title = a_img.get("title") 5# print(get_a_title) 6# 定义要下载的内容 7download = requests.get(get_a_src) 8# 循环打开文件创建jpg 9with open("image/" + get_a_title + ".jpg", mode="wb") as f: 10 # 开始下载 11 f.write(download.content) 12 if j > 0: 13 # 百分比 14 print("第", i - j, "张图片下载中-", format((i - j) / 25, '.2%')) 15 else: 16 print("第", i, "张图片下载中-", format(i / 25, '.2%')) 17# 停顿 18time.sleep(0.001)
三、图片转为二进制,保存MySQL数据库中
(1)、连接MySQL数据库
1def MySQL_connect(picture_id, picture_name, picture_href, picture_binary_system): 2 # 打开数据库连接 3 connection = pymysql.connect(host="****", user="****", password="****", port=****, database="****", 4 charset='utf8') 5 # MySQL语句 6 sql = 'INSERT INTO picture(picture_id,picture_name,picture_href,picture_binary_system) VALUES (%s,%s,%s,%s)' 7 # 获取标记 8 cursor = connection.cursor() 9 try: 10 # 执行SQL语句 11 cursor.execute(sql, [picture_id, picture_name, picture_href, picture_binary_system]) 12 # 提交事务 13 connection.commit() 14 except Exception as e: 15 print(str(e)) 16 # 有异常,回滚事务 17 connection.rollback() 18 # 释放内存 19 cursor.close() 20 connection.close()
(2)、循环图片,解析二进制,存储MySQL数据库中
1环打开图片 2with open("image/" + get_a_title + ".jpg", "rb") as f: 3 # 转为二进制格式,并且使用base64进行加密 4 base64_data = base64.b64encode(f.read()) 5 a = str(i) 6 b = (j + 1) 7 c = str(b) 8 # MySQL 9 MySQL_connect(c + "-" + a, get_a_src, get_a_title, base64_data)
四、附上完整代码
1import os 2import time 3import requests 4import pymysql 5import base64 6from bs4 import BeautifulSoup 7 8 9# 判断文件夹是否存在,不存在则创建 10def Judge_folder(): 11 folder = "image" 12 if not os.path.exists(folder): 13 print("文件不存在,已创建!") 14 os.mkdir(folder) 15 else: 16 print("开始下载图片") 17 18 19def Url_parsing(): 20 # url-网址 https://www.moyublog.com/95-2-0-0.html 148 21 # range(页数) 22 for i in range(1): 23 str_value = str(i) 24 # 页数 25 url = "https://www.moyublog.com/95-2-0-" + str_value + ".html" 26 # 浏览器类型-搜狗 27 Search_engine = {"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"} 28 # 发送请求,获取网址HTML,转为text 29 Type_conversion = requests.get(url=url, headers=Search_engine, timeout=None).text 30 # 定义BeautifulSoup,解析网址HTML 31 bs = BeautifulSoup(Type_conversion, 'html.parser') 32 # 获取指定div 33 scope_div = bs.find('div', attrs={'class': 'slist'}) 34 # 获取指定div中的所有a标签 35 scope_a = scope_div.findAll('a') 36 # print(scope_a) 37 j = i 38 # 循环获取指定div中的所有a标签 39 for int_A in scope_a: 40 # 清除不需要的a标签 41 if len(int_A) % 2 == 0: 42 # 获取定的a标签中的链接 43 get_href = int_A.get("href") 44 # print(get_href) 45 # 新的url-网址 46 url_A = get_href 47 # 发送请求,获取网址HTML,转为text,发送请求,获取网址HTML,转为text, 48 Type_conversion_A = requests.get(url=url_A, headers=Search_engine).text.encode('iso-8859-1').decode( 49 'utf-8') 50 # 定义新的BeautifulSoup,解析网址HTML 51 bs_A = BeautifulSoup(Type_conversion_A, 'html.parser') 52 # 定义新的网址中的div标签 53 A_div = bs_A.find('div', attrs={'class': 'photo-pic'}) 54 # 获取新定义div中的所有img标签 55 A_img = A_div.findAll('img') 56 # print(A_img)title 57 # 循环获取div中的所有img标签 58 for a_img in A_img: 59 # 百分比 60 i += 1 61 # 获取img标签中的src属性中的内容 62 get_a_src = a_img.get("src") 63 # 获取img标签中的title属性中的内容 64 get_a_title = a_img.get("title") 65 # print(get_a_title) 66 # 定义要下载的内容 67 download = requests.get(get_a_src) 68 # 循环打开文件创建jpg 69 with open("image/" + get_a_title + ".jpg", mode="wb") as f: 70 # 开始下载 71 f.write(download.content) 72 if j > 0: 73 # 百分比 74 print("第", i - j, "张图片下载中-", format((i - j) / 25, '.2%')) 75 else: 76 print("第", i, "张图片下载中-", format(i / 25, '.2%')) 77 # binary.close() 78 # 循环打开图片 79 with open("image/" + get_a_title + ".jpg", "rb") as f: 80 # 转为二进制格式,并且使用base64进行加密 81 base64_data = base64.b64encode(f.read()) 82 a = str(i) 83 b = (j + 1) 84 c = str(b) 85 # MySQL 86 MySQL_connect(c + "-" + a, get_a_src, get_a_title, base64_data) 87 print("第" + c + "页-第" + a + "张图片已存入MySQL数据库中!") 88 # 停顿 89 time.sleep(0.001) 90 91 print("第", j + 1, "页,下载完成!") 92 93 94def Exception_error(): 95 Judge_folder() 96 try: 97 Url_parsing() 98 except KeyboardInterrupt: 99 print('\n程序已终止. . . . .') 100 print('结束!') 101 102 103def MySQL_connect(picture_id, picture_name, picture_href, picture_binary_system): 104 # 打开数据库连接 105 connection = pymysql.connect(host="****", user="****", password="****", port=****, database="****", 106 charset='utf8') 107 # MySQL语句 108 sql = 'INSERT INTO picture(picture_id,picture_name,picture_href,picture_binary_system) VALUES (%s,%s,%s,%s)' 109 # 获取标记 110 cursor = connection.cursor() 111 try: 112 # 执行SQL语句 113 cursor.execute(sql, [picture_id, picture_name, picture_href, picture_binary_system]) 114 # 提交事务 115 connection.commit() 116 except Exception as e: 117 print(str(e)) 118 # 有异常,回滚事务 119 connection.rollback() 120 # 释放内存 121 cursor.close() 122 connection.close() 123 124 125if __name__ == '__main__': 126 Exception_error()

