python 获取图片并自动命名保存

# -* - coding: UTF-8 -* -
#导入第三方库
import urllib
from bs4 import BeautifulSoup
import requests
import os
import time
import random
# 获取文件夹,如果文件夹不存在则创建新文件夹
if os.path.isdir('E://biaoqing//'):
pass
else:
os.mkdir('E://biaoqing//')
get_url='http://qq.yh31.com'
i=58
while i>0:
print('第%s页:'%str(59 - int(i)))
local = "E:\\biaoqing\\" # 保存图片的文件夹
url = 'http://qq.yh31.com/ka/qw/List_%s.html'% i
Agent = [
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
]
User_Agent = random.choice(Agent)
headers = {'User-Agent': User_Agent}
html_ = requests.get(url, headers=headers)
html_.encoding = 'utf-8'
html_doc=html_.text
soup = BeautifulSoup(html_doc, 'lxml') # 解析 html_doc
# 获取所有img标签里面 属性border值为0的img标签
pss = soup.find_all('img',attrs={'border':0})
for ims in pss:
img_url = get_url + ims['src'] # 图片的路径
print(img_url)
mingcheng = '%s' % ims['alt'] # 图片命名所用,alt内容为图片名称
s = '%s' % ims['src'] # 获取字符 截取相应的名称及格式
geshi = s.split('.')[-1] # 图片命名所用,获取图片的格式
ming=mingcheng+'.'+geshi
urllib.request.urlretrieve(img_url, local + '%s'%ming)
time.sleep(0.5)
print('=====================================================')
i=i-1
time.sleep(2) # 设置休眠时间,访问频繁容易被封

转载于:https://www.cnblogs.com/ling-yu/p/9391563.html

你可能感兴趣的:(python 获取图片并自动命名保存)