pychram Process finished with exit code 0
import json
import os
from hashlib import md5
from urllib.parse import urlencode
from requests.exceptions import RequestException
import requests
def get_page(offset):
params = {
'offset': offset,
'format': 'json',
'keyword': '街拍',
'autoload': 'true',
'count': '20',
'cur_tab': 1,
'from': 'search_tab'
}
url = 'https://www.toutiao.com/api/search/content/?' + urlencode(params)
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
except RequestException:
print('请求索引页出错')
return None
def get_images(json):
data = json.get('data')
if data:
for item in data:
title = item.get(title)
image_list = item.get('image_list')
if image_list:
for image in image_list:
yield{
'image':image.get('url'),
'title':title
}
def save_image(item):
if not os.path.exists(item.get('title')):
os.mkdir(item.get('title'))
try:
local_image_url = item.get('image')
new_image_url = local_image_url.replace('list','large')
response = requests.get('http:'+new_image_url)
if response.status_code == 200:
file_path = '{0}/{1}{2}'.format(item.get('title'),md5(response.content).hexdigest(),'jpg')
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:
f.write(response.content)
else:
print('Already Downloaded', file_path)
except requests.ConnectionError:
print('Failed to Save Image')
哪里有问题给出报错信息 只定义没有调用,写多少行都没用。
页:
[1]