最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python把印象笔记帖子内容通过WordPress的REST的post接口去上传到crifan.com

印象笔记 crifan 2098浏览 0评论
折腾:
【未解决】用Python通过WordPress的REST的API发布文章post
期间,之前已经处理完毕印象笔记的帖子中的内容,处理成自己希望的html了:
2020/03/14 08:57:52 EvernoteToWordpress.py:942  INFO    postHtml=<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<html>
 <div>
  折腾:
 </div>
 <div>
  【未解决】Mac中pyenv设置系统python版本为3.7
 </div>
 <div>
  期间,去用pyenv安装python:
 </div>
 <div>
  <img src="https://www.crifan.com/files/pic/uploads/2020/03/f6956c30ef0b475fa2b99c2f49622e35.png">
  </img>
  <br/>
 </div>
 <pre class="brush: shell; gutter: true">➜  ~ pyenv install 3.7.3
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.3.tar.xz...
-&gt; https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz</pre>
 <div>
  pyenv install太慢
 </div>
 <div>
  所以参考
 </div>
 <div>
  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c">
   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 - 掘金
  </a>
 </div>
 <div>
  去换源,看看能否加速下载和安装
 </div>
 <pre class="brush: shell; gutter: true">➜  ~ mkdir ~/.pip
➜  ~ vim ~/.pip/pip.conf
➜  ~ cat ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com</pre>
 <div>
  <br/>
 </div>
 <div>
  此办法,并不能给pyenv install更换源以加速
 </div>
 <div>
  等后续看看pip安装的时,是否换了源,是否加了速度
 </div>
 <div>
  后来:
 </div>
 <div>
  【未解决】Mac中给Python 3.7中安装python库numpy和pandas
 </div>
 <div>
  期间,确认pip的源生效了,换成aliyun的地址了,下载速度很快:
 </div>
 <pre class="brush: shell; gutter: true">➜  ~ pip3 install numpy pandas
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting numpy
Downloading http://mirrors.aliyun.com/pypi/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl (15.1MB)
100% |████████████████████████████████| 15.1MB 1.1MB/s
Collecting pandas
Downloading http://mirrors.aliyun.com/pypi/packages/ab/ba/f97030b7e8ec0a981abdca173de4e727b3a7b4ed5dba492f362ba87d59a2/pandas-1.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (9.8MB)
100% |████████████████████████████████| 9.8MB 1.3MB/s
...</pre>
 <div>
  效果很不错。
 </div>
 <div>
  <br/>
 </div>
 <div>
  【总结】
 </div>
 <div>
  此处想要给pip全局下载加速,可以更换源。
 </div>
 <div>
  做法:
 </div>
 <pre class="brush: shell; gutter: true">mkdir ~/.pip
vim ~/.pip/pip.conf</pre>
 <div>
  保存内容:
 </div>
 <pre class="brush: shell; gutter: true">[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com</pre>
 <div>
  即可-》后续pip下载时,就会从
 </div>
 <div>
  <a href="http://mirrors.aliyun.com/">
   mirrors.aliyun.com
  </a>
 </div>
 <div>
  下载,速度很快。
 </div>
 <div>
  <br/>
 </div>
 <div>
  后记:
 </div>
 <div>
  貌似其他源也可以,比如:
 </div>
 <div>
  <a href="https://npm.taobao.org/mirrors/">
   taobao Mirrors
  </a>
 </div>
 <div>
  -&gt;
 </div>
 <div>
  <a href="https://npm.taobao.org/mirrors/python/">
   Python Mirror
  </a>
 </div>
 <div>
  抽空试试
 </div>
 <div>
  <br/>
 </div>
 <div>
  以及:
 </div>
 <div>
  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c">
   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 - 掘金
  </a>
 </div>
 <div>
  说的其他源:
 </div>
 <ul>
  <li>
   <div>
    豆瓣:http://pypi.douban.com
   </div>
  </li>
  <li>
   <div>
    阿里云:http://mirrors.aliyun.com/pypi/simple
   </div>
  </li>
  <li>
   <div>
    清华大学:https://pypi.tuna.tsinghua.edu.cn/simple
   </div>
  </li>
 </ul>
 <div>
  <br/>
 </div>
</html>
可以继续去发布了:
接下来就是去调用WordPress的rest的api去上传了。
具体怎么写代码,可以参考之前上传图片的具体写法。
Posts | REST API Handbook | WordPress Developer Resources
参考:
【已解决】研究WordPress的REST的api中创建POST时所需参数和格式
去写代码试试
def uploadPostToWordpress(curNote):
    """Upload note content new html to wordpress

    Args:
        curNote (Note): evernote Note
    Returns:
        (bool, dict)
    Raises:
    """
    uploadPostOk, respInfo = False, None

    curHeaders = {
        "Authorization": Authorization,
        "Content-Type": "application/json",
        "Accept": "application/json",
    }
    logging.info("curHeaders=%s", curHeaders)
    # 
    # created=1582809109000, updated=1584190363000,
    # dateTimestamp = curNote.updated
    dateTimestamp = curNote.created # 1582809109000
    dateTimestamp = float(dateTimestamp) / 1000.0 # 1582809109.0
    datetimeObj = utils.timestampToDatetime(dateTimestamp)
    outputFormat = "%Y%m%dT%H%M%S"
    dateStr = datetimeObj.strftime(format=outputFormat) # '20200227T211149'
    postDict = {
        "title": curNote.title,
        "content": curNote.content,
        # "date_gmt": dateStr,
        "date": dateStr,
        "slug": curNote.attributes.sourceURL,
        "status": "draft",
        "format": "standard",
        # TODO: featured_media, categories, tags, excerpt
    }
    logging.debug("postDict=%s", postDict)
    # postDict={'title': '【已解决】Mac中给pip更换源以加速下载', 'content': '<div>\n  折腾:\n </div>。。。。。。。。。</div>\n', 'date': '20200227T211149', 'slug': 'mac_pip_change_source_server_to_spped_up_download', 'status': 'draft', 'format': 'standard'}
    createPostUrl = API_POSTS
    resp = requests.post(
        createPostUrl,
        proxies=cfgProxies,
        headers=curHeaders,
        # data=json.dumps(postDict),
        json=postDict,
    )
    logging.info("resp=%s", resp)

    if resp.ok:
        respJson = resp.json()
        logging.info("respJson=%s", respJson)
        """

        """
        postIdInt = respJson["id"]
        postUrl = respJson["guid"]["rendered"]
        postLink = respJson["link"]
        logging.info("postIdInt=%s, postUrl=%s, postLink=%s", postIdInt, postUrl, postLink)
        # 
        uploadPostOk = True
        respInfo = {
            "id": postIdInt,
            "url": imgUrl,
            "link": postLink,
        }
    else:
        uploadPostOk = False
        respInfo = {
            "errCode": resp.status_code,
            "errMsg": resp.text
        }

    logging.info("uploadPostOk=%s, respInfo=%s", uploadPostOk, respInfo)
    return uploadPostOk, respInfo
结果:
【已解决】Python上传WordPress的post帖子报错:400 rest_invalid_param 无效参数 date的类型不是string
就上传成功了。
返回内容:
2020/03/15 10:04:36 EvernoteToWordpress.py:788  INFO    respJson={'id': 70410, 'date': '2020-02-27T21:11:49', 'date_gmt': '2020-02-27T13:11:49', 'guid': {'rendered': 'https://www.crifan.com/?p=70410', 'raw': 'https://www.crifan.com/?p=70410'}, 'modified': '2020-02-27T21:11:49', 'modified_gmt': '2020-02-27T13:11:49', 'password': '', 'slug': 'mac_pip_change_source_server_to_spped_up_download', 'status': 'draft', 'type': 'post', 'link': 'https://www.crifan.com/?p=70410', 'title': {'raw': '【已解决】Mac中给pip更换源以加速下载', 'rendered': '【已解决】Mac中给pip更换源以加速下载'}, 'content': {'raw': '<div>\n  折腾:\n </div>\n <div>\n  【未解决】Mac中pyenv设置系统python版本为3.7\n </div>\n <div>\n  期间,去用pyenv安装python:\n </div>\n <div>\n  <img src="https://www.crifan.com/files/pic/uploads/2020/03/675e29f778ab4b31be41965b7f55dc91.jpg"/>\n  \n </div>\n <div>\n  日志:\n </div>\n <pre class="brush: shell; gutter: true">➜\xa0\xa0~ pyenv install 3.7.3\npython-build: use openssl from homebrew\npython-build: use readline from homebrew\nDownloading Python-3.7.3.tar.xz...\n-&gt; https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz</pre>\n <div>\n  pyenv install太慢\n </div>\n <div>\n  所以参考\n </div>\n <div>\n  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c">\n   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 - 掘金\n  </a>\n </div>\n <div>\n  去换源,看看能否加速下载和安装\n </div>\n <pre class="brush: shell; gutter: true">➜\xa0\xa0~ mkdir ~/.pip\n➜\xa0\xa0~ vim ~/.pip/pip.conf\n➜\xa0\xa0~ cat ~/.pip/pip.conf\n[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host=mirrors.aliyun.com</pre>\n <div>\n </div>\n <div>\n  此办法,并不能给pyenv install更换源以加速\n </div>\n <div>\n  等后续看看pip安装的时,是否换了源,是否加了速度\n </div>\n <div>\n  后来:\n </div>\n <div>\n  【未解决】Mac中给Python 3.7中安装python库numpy和pandas\n </div>\n <div>\n  期间,确认pip的源生效了,换成aliyun的地址了,下载速度很快:\n </div>\n <pre class="brush: shell; gutter: true">➜\xa0\xa0~ pip3 install numpy pandas\nLooking in indexes: http://mirrors.aliyun.com/pypi/simple/\nCollecting numpy\nDownloading http://mirrors.aliyun.com/pypi/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl (15.1MB)\n100% |████████████████████████████████| 15.1MB 1.1MB/s\nCollecting pandas\nDownloading http://mirrors.aliyun.com/pypi/packages/ab/ba/f97030b7e8ec0a981abdca173de4e727b3a7b4ed5dba492f362ba87d59a2/pandas-1.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (9.8MB)\n100% |████████████████████████████████| 9.8MB 1.3MB/s\n...</pre>\n <div>\n  效果很不错。\n </div>\n <div>\n </div>\n <div>\n  【总结】\n </div>\n <div>\n  此处想要给pip全局下载加速,可以更换源。\n </div>\n <div>\n  做法:\n </div>\n <pre class="brush: shell; gutter: true">mkdir ~/.pip\nvim ~/.pip/pip.conf</pre>\n <div>\n  保存内容:\n </div>\n <pre class="brush: shell; gutter: true">[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host=mirrors.aliyun.com</pre>\n <div>\n  即可-》后续pip下载时,就会从\n </div>\n <div>\n  <a href="http://mirrors.aliyun.com/">\n   mirrors.aliyun.com\n  </a>\n </div>\n <div>\n  下载,速度很快。\n </div>\n <div>\n </div>\n <div>\n  后记:\n </div>\n <div>\n  貌似其他源也可以,比如:\n </div>\n <div>\n  <a href="https://npm.taobao.org/mirrors/">\n   taobao Mirrors\n  </a>\n </div>\n <div>\n  -&gt;\n </div>\n <div>\n  <a href="https://npm.taobao.org/mirrors/python/">\n   Python Mirror\n  </a>\n </div>\n <div>\n  抽空试试\n </div>\n <div>\n </div>\n <div>\n  以及:\n </div>\n <div>\n  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c">\n   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 - 掘金\n  </a>\n </div>\n <div>\n  说的其他源:\n </div>\n <ul>\n  <li>\n   <div>\n    豆瓣:http://pypi.douban.com\n   </div>\n  </li>\n  <li>\n   <div>\n    阿里云:http://mirrors.aliyun.com/pypi/simple\n   </div>\n  </li>\n  <li>\n   <div>\n    清华大学:https://pypi.tuna.tsinghua.edu.cn/simple\n   </div>\n  </li>\n </ul>\n <div>\n </div>\n', 'rendered': '<div>\n  折腾:\n </div>\n<div>\n  【未解决】Mac中pyenv设置系统python版本为3.7\n </div>\n<div>\n  期间,去用pyenv安装python:\n </div>\n<div>\n  <img src="https://www.crifan.com/files/pic/uploads/2020/03/675e29f778ab4b31be41965b7f55dc91.jpg"/></p></div>\n<div>\n  日志:\n </div>\n<pre class="brush: shell; gutter: true">➜\xa0\xa0~ pyenv install 3.7.3\npython-build: use openssl from homebrew\npython-build: use readline from homebrew\nDownloading Python-3.7.3.tar.xz...\n-&gt; https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz</pre>\n<div>\n  pyenv install太慢\n </div>\n<div>\n  所以参考\n </div>\n<div>\n  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c"><br />\n   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 &#8211; 掘金<br />\n  </a>\n </div>\n<div>\n  去换源,看看能否加速下载和安装\n </div>\n<pre class="brush: shell; gutter: true">➜\xa0\xa0~ mkdir ~/.pip\n➜\xa0\xa0~ vim ~/.pip/pip.conf\n➜\xa0\xa0~ cat ~/.pip/pip.conf\n[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host=mirrors.aliyun.com</pre>\n<div>\n </div>\n<div>\n  此办法,并不能给pyenv install更换源以加速\n </div>\n<div>\n  等后续看看pip安装的时,是否换了源,是否加了速度\n </div>\n<div>\n  后来:\n </div>\n<div>\n  【未解决】Mac中给Python 3.7中安装python库numpy和pandas\n </div>\n<div>\n  期间,确认pip的源生效了,换成aliyun的地址了,下载速度很快:\n </div>\n<pre class="brush: shell; gutter: true">➜\xa0\xa0~ pip3 install numpy pandas\nLooking in indexes: http://mirrors.aliyun.com/pypi/simple/\nCollecting numpy\nDownloading http://mirrors.aliyun.com/pypi/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl (15.1MB)\n100% |████████████████████████████████| 15.1MB 1.1MB/s\nCollecting pandas\nDownloading http://mirrors.aliyun.com/pypi/packages/ab/ba/f97030b7e8ec0a981abdca173de4e727b3a7b4ed5dba492f362ba87d59a2/pandas-1.0.1-cp37-cp37m-macosx_10_9_x86_64.whl (9.8MB)\n100% |████████████████████████████████| 9.8MB 1.3MB/s\n...</pre>\n<div>\n  效果很不错。\n </div>\n<div>\n </div>\n<div>\n  【总结】\n </div>\n<div>\n  此处想要给pip全局下载加速,可以更换源。\n </div>\n<div>\n  做法:\n </div>\n<pre class="brush: shell; gutter: true">mkdir ~/.pip\nvim ~/.pip/pip.conf</pre>\n<div>\n  保存内容:\n </div>\n<pre class="brush: shell; gutter: true">[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host=mirrors.aliyun.com</pre>\n<div>\n  即可-》后续pip下载时,就会从\n </div>\n<div>\n  <a href="http://mirrors.aliyun.com/"><br />\n   mirrors.aliyun.com<br />\n  </a>\n </div>\n<div>\n  下载,速度很快。\n </div>\n<div>\n </div>\n<div>\n  后记:\n </div>\n<div>\n  貌似其他源也可以,比如:\n </div>\n<div>\n  <a href="https://npm.taobao.org/mirrors/"><br />\n   taobao Mirrors<br />\n  </a>\n </div>\n<div>\n  -&gt;\n </div>\n<div>\n  <a href="https://npm.taobao.org/mirrors/python/"><br />\n   Python Mirror<br />\n  </a>\n </div>\n<div>\n  抽空试试\n </div>\n<div>\n </div>\n<div>\n  以及:\n </div>\n<div>\n  <a href="https://juejin.im/post/5c739c86e51d45699514ee0c"><br />\n   MacOS 下利用 pyenv 管理Python 版本和虚拟环境 &#8211; 掘金<br />\n  </a>\n </div>\n<div>\n  说的其他源:\n </div>\n<ul>\n<li>\n<div>\n    豆瓣:http://pypi.douban.com\n   </div>\n</li>\n<li>\n<div>\n    阿里云:http://mirrors.aliyun.com/pypi/simple\n   </div>\n</li>\n<li>\n<div>\n    清华大学:https://pypi.tuna.tsinghua.edu.cn/simple\n   </div>\n</li>\n</ul>\n<div>\n </div>\n<p>转载请注明:<a href="https://www.crifan.com">在路上</a> &raquo; <a href="https://www.crifan.com/?p=70410">【已解决】Mac中给pip更换源以加速下载</a></p>', 'protected': False, 'block_version': 0}, 'excerpt': {'raw': '', 'rendered': '<p>折腾: 【未解决】Mac中pyenv设置系统python版本为3.7 期间,去用pyenv安装python:  [&hellip;]</p>\n', 'protected': False}, 'author': 1, 'featured_media': 0, 'comment_status': 'open', 'ping_status': 'open', 'sticky': False, 'template': '', 'format': 'standard', 'meta': [], 'categories': [1], 'tags': [], 'permalink_template': 'https://www.crifan.com/%postname%/', 'generated_slug': '【已解决】mac中给pip更换源以加速下载', '_links': {'self': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'collection': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts'}], 'about': [{'href': 'https://www.crifan.com/wp-json/wp/v2/types/post'}], 'author': [{'embeddable': True, 'href': 'https://www.crifan.com/wp-json/wp/v2/users/1'}], 'replies': [{'embeddable': True, 'href': 'https://www.crifan.com/wp-json/wp/v2/comments?post=70410'}], 'version-history': [{'count': 0, 'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410/revisions'}], 'wp:attachment': [{'href': 'https://www.crifan.com/wp-json/wp/v2/media?parent=70410'}], 'wp:term': [{'taxonomy': 'category', 'embeddable': True, 'href': 'https://www.crifan.com/wp-json/wp/v2/categories?post=70410'}, {'taxonomy': 'post_tag', 'embeddable': True, 'href': 'https://www.crifan.com/wp-json/wp/v2/tags?post=70410'}], 'wp:action-publish': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-unfiltered-html': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-sticky': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-assign-author': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-create-categories': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-assign-categories': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-create-tags': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'wp:action-assign-tags': [{'href': 'https://www.crifan.com/wp-json/wp/v2/posts/70410'}], 'curies': [{'name': 'wp', 'href': 'https://api.w.org/{rel}', 'templated': True}]}}
去格式化看看
主要内容是:
{
  "id": 70410,
  "date": "2020-02-27T21:11:49",
  "date_gmt": "2020-02-27T13:11:49",
  "guid": {
    "rendered": "https://www.crifan.com/?p=70410",
    "raw": "https://www.crifan.com/?p=70410"
  },
  "modified": "2020-02-27T21:11:49",
  "modified_gmt": "2020-02-27T13:11:49",
  "password": "",
  "slug": "mac_pip_change_source_server_to_spped_up_download",
  "status": "draft",
  "type": "post",
  "link": "https://www.crifan.com/?p=70410",
  "title": {
    'raw": "【已解决】Mac中给pip更换源以加速下载", "rendered": "【已解决】Mac中给pip更换源以加速下载'
  },
  "content": {
...
打开看看:
https://www.crifan.com/?p=70410
https://www.crifan.com/mac_pip_change_source_server_to_spped_up_download
找不到,去后台看看,发现是:处于草稿状态:
是正常的。
去发布后,效果是:
【已解决】Mac中给pip更换源以加速下载 – 在路上
去看看图片:
https://www.crifan.com/files/pic/uploads/2020/03/675e29f778ab4b31be41965b7f55dc91.jpg
是正常的:
后面内容:
也是正常的。
总体上:codeblock变pre,brush设置为shell
其他代码是普通的html
再去后台看看当前html是什么
列表是我们希望的:
<ul>
     <li>
<div>豆瓣:http://pypi.douban.com</div></li>
     <li>
<div>阿里云:http://mirrors.aliyun.com/pypi/simple</div></li>
     <li>
<div>清华大学:https://pypi.tuna.tsinghua.edu.cn/simple</div></li>
</ul>
pre也是正常的:
至此,是正常发布帖子内容到自己的WordPress的crifan.com网站中了。
【总结】
此处用代码:
HOST = "https://www.crifan.com"
API_MEDIA = HOST + "/wp-json/wp/v2/media"
API_POSTS = HOST + "/wp-json/wp/v2/posts" # https://www.crifan.com/wp-json/wp/v2/posts

def uploadPostToWordpress(curNote):
    """Upload note content new html to wordpress

    Args:
        curNote (Note): evernote Note
    Returns:
        (bool, dict)
    Raises:
    """
    uploadPostOk, respInfo = False, None

    curHeaders = {
        "Authorization": Authorization,
        "Content-Type": "application/json",
        "Accept": "application/json",
    }
    logging.info("curHeaders=%s", curHeaders)
    # 
    # created=1582809109000, updated=1584190363000,
    # dateTimestamp = curNote.updated
    dateTimestamp = curNote.created # 1582809109000
    dateTimestamp = float(dateTimestamp) / 1000.0 # 1582809109.0
    datetimeObj = utils.timestampToDatetime(dateTimestamp)
    outputFormat = "%Y-%m-%dT%H:%M:%S"
    dateStr = datetimeObj.strftime(format=outputFormat) # '2020-02-27T21:11:49'
    postDict = {
        "title": curNote.title,
        "content": curNote.content,
        # "date_gmt": dateStr,
        "date": dateStr,
        "slug": curNote.attributes.sourceURL,
        "status": "draft",
        "format": "standard",
        # TODO: featured_media, categories, tags, excerpt
    }
    logging.debug("postDict=%s", postDict)
    # postDict={'title': '【已解决】Mac中给pip更换源以加速下载', 'content': '<div>\n  折腾:\n </div>。。。。。。。。。</div>\n', 'date': '20200227T211149', 'slug': 'mac_pip_change_source_server_to_spped_up_download', 'status': 'draft', 'format': 'standard'}
    createPostUrl = API_POSTS
    resp = requests.post(
        createPostUrl,
        proxies=cfgProxies,
        headers=curHeaders,
        # data=json.dumps(postDict),
        json=postDict,
    )
    logging.info("resp=%s", resp)

    if resp.ok:
        respJson = resp.json()
        logging.info("respJson=%s", respJson)
        """
            {
                "id": 70410,
                "date": "2020-02-27T21:11:49",
                "date_gmt": "2020-02-27T13:11:49",
                "guid": {
                    "rendered": "https://www.crifan.com/?p=70410",
                    "raw": "https://www.crifan.com/?p=70410"
                },
                "modified": "2020-02-27T21:11:49",
                "modified_gmt": "2020-02-27T13:11:49",
                "password": "",
                "slug": "mac_pip_change_source_server_to_spped_up_download",
                "status": "draft",
                "type": "post",
                "link": "https://www.crifan.com/?p=70410",
                "title": {
                    'raw": "【已解决】Mac中给pip更换源以加速下载",
                    "rendered": "【已解决】Mac中给pip更换源以加速下载"
                },
                "content": {
                    ...
        """
        postIdInt = respJson["id"]
        postUrl = respJson["guid"]["rendered"]
        postLink = respJson["link"]
        logging.info("postIdInt=%s, postUrl=%s, postLink=%s", postIdInt, postUrl, postLink)
        # 
        uploadPostOk = True
        respInfo = {
            "id": postIdInt,
            "url": imgUrl,
            "link": postLink,
        }
    else:
        uploadPostOk = False
        respInfo = {
            "errCode": resp.status_code,
            "errMsg": resp.text
        }


    logging.info("uploadPostOk=%s, respInfo=%s", uploadPostOk, respInfo)
    return uploadPostOk, respInfo
即可正常的把html即相关内容,通过WordPress的REST的api,即:
POST /wp-json/wp/v2/posts
新建,即发布对应文章。
返回后的内容是json,格式化后大概是:
{
                "id": 70410,
                "date": "2020-02-27T21:11:49",
                "date_gmt": "2020-02-27T13:11:49",
                "guid": {
                    "rendered": "https://www.crifan.com/?p=70410",
                    "raw": "https://www.crifan.com/?p=70410"
                },
                "modified": "2020-02-27T21:11:49",
                "modified_gmt": "2020-02-27T13:11:49",
                "password": "",
                "slug": "mac_pip_change_source_server_to_spped_up_download",
                "status": "draft",
                "type": "post",
                "link": "https://www.crifan.com/?p=70410",
                "title": {
                    'raw": "【已解决】Mac中给pip更换源以加速下载",
                    "rendered": "【已解决】Mac中给pip更换源以加速下载"
                },
                "content": {
                    ...
供参考。

转载请注明:在路上 » 【已解决】Python把印象笔记帖子内容通过WordPress的REST的post接口去上传到crifan.com

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.206 seconds, using 22.26MB memory