前几个月花瓣网突然抄袭了pinterest更新网站,一直以为影响不大,直到最近浏览notion的花瓣时,才发现了花瓣网也开启了防盗链,于是寻思着把notion上的图片链接改为自己的图床
不过notion api一贯不太好用,只好f12查找需要的block
1.先提取block内的图片链接
代码
import requests
import json
filepath1 = "k:/office/py/爬虫/花瓣/插画.json"
with open(filepath1, "r", encoding="utf-8") as f:
row_data=json.loads(f.readline())
a=0
while a < 200:
i=row_data["result"]["reducerResults"]["collection_group_results"]["blockIds"][a]
block=row_data['recordMap']['block'][i]["value"]["content"][0]
links=row_data['recordMap']['block'][block]["value"]["properties"]["title"][0][0]
with open("k:/office/py/爬虫/花瓣/p-block.csv", "a",encoding="utf-8", newline="")as fo:
fo.write(block+","+links+","+i+"\n")
a+=1
提取出来大致是这样
花瓣链接要整理下
但如果图片超过200个,那么只能先提取block,在根据api提取图片链接
代码
import requests
import json
import csv
import time
class photo():
def add_photo(urls):
r = requests.request(
"GET",
"https://api.notion.com/v1/blocks/"+urls,
headers={"Authorization": "Bearer " + "自己token", "Notion-Version": "2022-02-22"},
)
row_data=json.loads(r.text)
print(r.text)
time.sleep(5)
try:
i=row_data["image"]["external"]["url"]
b=row_data["parent"]["page_id"]
with open("k:/office/py/爬虫/花瓣/插画3-block.csv", "a",encoding="utf-8", newline="")as fo:
fo.write(b+","+i+"\n")
except:
print("异常")
filepath="k:/office/py/爬虫/花瓣/插画-block.csv"
with open(filepath, "r", encoding="utf-8", newline="") as f:
csvreader1 = csv.reader(f)
for row2 in csvreader1:
urls=row2[0]
photo.add_photo(urls)
2.根据图片链接下载图片
代码
import requests
import os
import csv
class photo():
def add_photo(urls):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4843.400 QQBrowser/9.7.13021.400'}
r = requests.get("https://hbimg.huabanimg.com/"+urls+"_fw658", headers=headers)
filepath1 = "k:/office/py/爬虫/花瓣/插画_photo"+"/"+urls+".webp"
with open(filepath1, "wb") as f:
f.write(r.content)
filepath="k:/office/py/爬虫/花瓣/插画3-block.csv"
with open(filepath, "r", encoding="utf-8", newline="") as f:
csvreader1 = csv.reader(f)
for row2 in csvreader1:
urls=row2[1]
photo.add_photo(urls)
3.更新图片
由于无法直接在block更新图片,只能删除原来block,在添加新的图片
代码
import requests
import csv
class notionDemo():
def add_bill(a,b,c):
body = {
"children":[
{
'object': 'block',
"type": "image",
"image":{
"type":"external",
"external":{
"url":"https:/4xu.xyz/"+c+".webp"
}
}
}]
}
r = requests.request(
"Patch",
"https://api.notion.com/v1/blocks/"+a+"/children",
json=body,
headers={"Authorization": "Bearer " + "自己token", "Notion-Version": "2022-02-22"},
)
print(r.text)
r = requests.request(
"DELETE",
"https://api.notion.com/v1/blocks/"+b,
headers={"Authorization": "Bearer " + "自己token", "Notion-Version": "2022-02-22"},
)
filepath="k:/office/py/爬虫/花瓣/插画3-block.csv"
with open(filepath, "r", encoding="utf-8", newline="") as f:
csvreader1 = csv.reader(f)
for row2 in csvreader1:
a=row2[2]
b=row2[0]
c=row2[1]
notionDemo.add_bill(a,b,c)
写完后,还是熟悉的