Created
February 18, 2020 04:49
-
-
Save growvv/7a8931c2c0b8d85430cad891a8aaf364 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
from urllib.parse import quote, unquote | |
import os | |
def down_pic(pic_urls): | |
"""给出图片链接列表, 下载所有图片""" | |
for i, pic_url in enumerate(pic_urls): | |
try: | |
pic = requests.get(pic_url, timeout=15) | |
string = os.path.basename(pic_url) | |
with open(string, 'wb') as f: | |
f.write(pic.content) | |
print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url))) | |
except Exception as e: | |
print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url))) | |
print(e) | |
continue | |
keyword = '新恒结衣' | |
url = 'http://image.baidu.com/search/avatarjson?tn=resultjsonavatarnew&ie=utf-8&word=' + quote(keyword) + '&cg=star&pn=120&rn=30&itg=0&z=0&fr=&width=&height=&lm=-1&ic=0&s=0&st=-1&gsm=78' | |
s=requests.session() | |
headers = {'Content-Type': 'application/json'} | |
response = s.get(url,headers=headers) | |
data = json.loads(response.text) | |
#print(data) | |
pic_urls = [] | |
num = len(data['imgs']) | |
for i in range(num): | |
print(data['imgs'][i]['objURL']) | |
pic_urls.append(data['imgs'][i]['objURL']) | |
down_pic(pic_urls) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment