json 定义 怎么解决呀
def loop():for i in range(3):
url = 'https://unsplash.com/t/travel?per_page=12&page=' + str(i)
print(F'page = {i},url = {url}')
response = json.loads(send_get(url,None))
deal_result(response,i)
if __name__== '__main__':
loop() 有点不知道你在问啥,看看这个能否帮到你json 代码发全 suchocolate 发表于 2021-9-29 09:50
代码发全
from os import times
import urllib.request
import requests
from requests.api import request
from requests.models import Response
def download_img(img_url,file_name):
print(F'downloading {file_name},img_url = {img_url}')
request = urllib.request.Request(img_url)
try:
response = urllib.request.urlopen(request)
if (response.getcode()== 200):
with open (file_name,"wb") as f:
f.write(response.read()) #将内容写入图片
return 'ok'
except:
return "fail"
#处理结果集
def deal_result(result,page):
index = page * 12 # 每页有12张图片,所以这里需要转换
for i in range(len(result)):
img_url = result['urls']['full']
index += 1
download_img(img_url,str(index) + '.png')
import requests as req
#发送请求
def send_get(url,params):
times.sleep(2)
response = req.get(url,headers=None,params=params)
return response.text
# 循环翻页
def loop():
for i in range(3):
url = 'https://unsplash.com/t/travel?per_page=12&page=' + str(i)
print(F'page = {i},url = {url}')
response = json.loads(send_get(url,None))
deal_result(response,i)
if __name__== '__main__':
loop() 尬了 {:10_266:} 手里尖刀刺 发表于 2021-9-29 11:37
尬了
json 要导入 json 库
大马强 发表于 2021-9-29 09:46
有点不知道你在问啥,看看这个能否帮到你json
谢谢学习分享 手里尖刀刺 发表于 2021-9-29 15:53
谢谢学习分享
{:10_262:}竟然是忘导入库
页:
[1]