import requests as req
from bs4 import BeautifulSoup
import re
import os
is_proxy = True #是否加载代理
user = '123456'
password = '123456'
http = "http://{__user}:{__password}@10.191.131.43:3128".format(__user=user,__password=password)
proxy = {
"http":http ,
"https": http,
}
if(not is_proxy):
proxy = None
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'
}
domain = "http://api.btstu.cn/sjbz/?lx=dongman"
#判断建立img文件夹
img_dir = "./img"
exists_flag = os.path.exists(img_dir)
if(not exists_flag):
os.makedirs(img_dir)
def download_img(img_download_url,proxy,header):
img_res = req.get(img_download_url,proxies=proxy,headers=header)
img_byte = img_res.content
img_url = img_res.request.url
reg = r'[^/]+\.[^/]+$'
img_name = re.search(reg,img_url).group()
download_path = './img/'+img_name
with open(download_path,mode="wb") as f:
f.write(img_byte)
# download_img(domain,proxy,header)
def get_file_name_list_by_path(file_dir):
file_name_list = []
for files in os.listdir(file_dir):
if os.path.isfile(os.path.join(file_dir,files)):
file_name_list.append(files)
return file_name_list
download_num = 10 #下载图片数量
for index in range(download_num):
download_img(domain,proxy,header)
#打印图片列表
print(get_file_name_list_by_path("./img"))
新浪接口url:http://api.btstu.cn/sjbz/?lx=dongman 会自动返回一张动漫图