1、 需求概要
使用MidJourney時,
在https://www.midjourney.com/app/這里有接口https://www.midjourney.com/api/app/recent-jobs/?amount=35&dedupe=true&jobStatus=completed&jobType=upscale&orderBy=new&page=3&prompt=undefined&refreshApi=0&searchType=advanced&service=null&toDate=2023-06-16+09%3A50%3A17.379092&type=all&userId=b12e169c-f609-4fd6-b917-11c2deaa8cff&user_id_ranked_score=null&_ql=todo&_qurl=https%3A%2F%2Fwww.midjourney.com%2Fapp%2F,返回的json串中有圖片的相關(guān)信息,要把整個json串都存下來。
2、 實現(xiàn)思路
(1)在翻頁的時候,接口中的page值會發(fā)生變化,所以可以通過循環(huán)遍歷page的值來實現(xiàn)爬取頁面信息,目前發(fā)現(xiàn)page最大的值為50,所以可以 for page in range(50)
(2)接口中還有很多屬性,如下:
屬性名 | 值 | 猜測作用 |
---|---|---|
amount | 35 | 一頁包含的圖片數(shù)量(目測最大50) |
dedupe | true | 去重(是) |
jobStatus | completed | 圖片狀態(tài)(完成) |
jobType | upscale | 圖片類型(高級),對應(yīng)頁面上的All,Grids(網(wǎng)格狀),Upscales |
orderBy | new | 排序(按最新),對應(yīng)頁面上的Hot,New,Top,F(xiàn)avorited |
page | 3 | 當(dāng)前頁碼(目測最大為50) |
prompt | undefined | 提示(未定義) |
refreshApi | 0 | |
searchType | advanced | |
service | null | |
toDate | 2023-06-16 09:50:17.379092 | 這個等會得細(xì)看下,這個屬性貌似只有orderby為new的時候會有 |
type | all | |
userId | b12e169c-f609-4fd6-b917-11c2deaa8cff | |
user_id_ranked_score | null | |
_ql | todo | |
_qurl | https://www.midjourney.com/app/ | |
isPublished | true | 這個對應(yīng)頁面上的ispublished選項(感覺不用特殊設(shè)置) |
通過改變某些屬性的值也許能找到相對正確的爬取策略
(3)可以簡單地將爬取到的大量json串保存在txt中,等待后續(xù)的處理文章來源:http://www.zghlxwxcb.cn/news/detail-644061.html
3、具體實現(xiàn)
通過研究發(fā)現(xiàn),orderby使用new或者enqueue_time的時候,再加上toDate參數(shù),就會返回固定的json串,page最大值為50,amount最大值為50,也就是說,每次循環(huán)50次page最多爬取到50*50=2500張圖,然后再取最后得到的圖片的enqueue_time為新的toDate,進行下一輪的50次循環(huán)爬取,如此反復(fù),就可以實現(xiàn)將某日前的所有歷史圖片數(shù)據(jù)全部爬取下來。注意每次爬取間睡個幾秒,免得被封掉。文章來源地址http://www.zghlxwxcb.cn/news/detail-644061.html
4、代碼
import json
import requests
import random
import time
#
# _ooOoo_
# o8888888o
# 88" . "88
# (| -_- |)
# O\ = /O
# ____/`---'\____
# .' \\| |// `.
# / \\||| : |||// \
# / _||||| -:- |||||- \
# | | \\\ - /// | |
# | \_| ''\---/'' | |
# \ .-\__ `-` ___/-. /
# ___`. .' /--.--\ `. . __
# ."" '< `.___\_<|>_/___.' >'"".
# | | : `- \`.;`\ _ /`;.`/ - ` : | |
# \ \ `-. \_ __\ /__ _/ .-` / /
# ======`-.____`-.___\_____/___.-`____.-'======
# `=---='
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 佛祖保佑 永無bug
# 成功爬取 不會被ban
# 構(gòu)建請求頭:
headers = {
# 這里需要自己構(gòu)建請求頭
}
# 切分url
big_pre_url = f"https://www.midjourney.com/api/app/recent-jobs/?amount=50&dedupe=true&jobStatus=completed&orderBy=enqueue_time&page="
# 維護一個數(shù)組,用于循環(huán)
# 需要填入開始爬取的日期
# 可以使用datatime操作時間參數(shù),這里簡單點就用字符串就可以
toDate = ['']
# 創(chuàng)建一個標(biāo)記符,用于控制循環(huán)結(jié)束
tag = True
# 開始循環(huán)爬取
print("開始爬取")
while tag:
try:
# 拆分url
small_pre_url = f"&prompt=undefined&refreshApi=0&searchType=advanced&service=null&toDate=2023-"
small_after_url = f"+07%3A21%3A03.591348&type=all&userId=b12e169c-f609-4fd6-b917-11c2deaa8cff&user_id_ranked_score=null&_ql=todo&_qurl=https%3A%2F%2Fwww.midjourney.com%2Fapp%2F"
# 從維護的數(shù)組中更新url,進行循環(huán)爬取
big_after_url = small_pre_url + toDate[-1] + small_after_url
# 內(nèi)部循環(huán)page從0到50
for page in range(1, 51):
print("正在爬取toDate為:" + str(toDate[-1]) + ",page為:" + str(page))
# 睡一小會,免得號沒了
time.sleep(random.randint(5, 10))
# 拼接url
url = big_pre_url + str(page) + big_after_url
# 發(fā)出請求得到響應(yīng)
response = requests.get(url=url, headers=headers).json()
# json入庫
with open("images_json.txt", "a+", encoding="utf-8") as f:
f.write(str(response) + '\n')
# 維護下一個循環(huán)
new_toDate = response[49]["enqueue_time"][5:10:]
# 將最新的數(shù)據(jù)插到toDate中用于下一輪循環(huán)
toDate.append(new_toDate)
except Exception as e:
tag = False
print("-----------------出現(xiàn)異常,終止循環(huán)-----------------")
print("異常信息為:" + e)
print("當(dāng)前 toDate:" + str(toDate[-1]) + "當(dāng)前page:" + str(page))
到了這里,關(guān)于Python爬取MidJourney歷史圖片【僅供參考學(xué)習(xí)使用】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!