我有一个页面,需要定期更新文件位置,以便链接指向新文件。尝试通过使用requests
.
我已经通过“应用程序密码”插件设置了身份验证,并且我能够从api中为所讨论的页面下载json。我有一个包含新文件位置的预构建字符串,可以使用了。我将页面内容,特别是“呈现”版本,提取为字符串。我做了一个快速replace()
在两个已知标记之间的字符串上(<h4>
) 然后将修改后的“呈现”页面内容重新加载回json,并将其上载回端点,再次使用requests()
.
当这一切都说了又做了,我没有从脚本中得到任何错误,状态代码为200。。。但页面上没有任何变化。
此外,我在错误日志中发现以下内容:
[2019年8月21日00:12:51 UTC]PHP警告:在/home1/example/public\\u html/wp includes/rest api/class wp rest请求中为foreach()提供的参数无效。php在线778
不太清楚那是什么意思,但我想这不好。
import base64, json, requests
user = \'admin\'
pythonapp = \'QUDM PYFn xfyK 9Hhe Ayf2 hk6k\'
url = \'https://example.com/wp-json/wp/v2\'
data_string = user + \':\' + pythonapp
token = base64.b64encode(data_string.encode())
headers = {\'Authorization\':\'Basic \' + token.decode(\'utf8\')}
newsletterURL = \'http://www.example.com/wp-content/uploads/2019/08/lorem-ipsum.pdf\'
oldH4 = \'<h4><a href="http://www.example.com/wp-content/uploads/2019/08/NCWGC-2019-8.pdf" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Click here to download the newsletter!!!</a></h4>\'
newH4 = \'<h4><a href="\' + newsletterURL + \'" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Click here to download the newsletter!!!</a></h4>\'
# Download the current Newsletter page
r = requests.get(url + \'/pages/6\', headers=headers)
tmp = r.json()
非常确定到目前为止的代码按照预期/预期工作;显然,它经过了一些修改,成为了一个独立的示例,而不是更大脚本的一部分。
oldPageContent = tmp[\'content\'][\'rendered\']
# print(oldPageContent.partition(\'</h4>\')[0])
newPageContent = oldPageContent.replace(oldH4, newH4)
# print(newPageContent.partition(\'</h4>\')[0])
tmp[\'content\'][\'rendered\'] = newPageContent
# print(tmp[\'content\'][\'rendered\'].partition(\'</h4>\')[0])
r = requests.post(url + \'/pages/6\', headers=headers, json=tmp)
# print(json.loads(r.content.decode(\'utf8\'))[\'content\'][\'rendered\'].partition(\'</h4>\')
在这里,我试图替换正文内容中的特定URL,重新组合内容,并将整个内容上传回
"content": {
"rendered": "\\n<h4><a href=\\"http://www.example.com/wp-content/uploads/2019/08/NCWGC-2019-8.pdf\\" target=\\"_blank\\" rel=\\"noreferrer noopener\\" aria-label=\\" (opens in a new tab)\\">Click here to download the newsletter!!!</a></h4>\\n\\n\\n\\n<p>Submissions for the newsletter are always welcome. Space is somewhat limited so keep \\u2019em short and sweet!",
"protected": false
}
这是我试图从页面内容中提取、更新和上载的特定字段的一个示例。
我在想也许我应该json.dumps/loads
获取[\'content\'][\'rendered\']
而不仅仅是r.json()
? 虽然当我打印出结果字符串时,它们看起来确实像是对的。。。
有没有关于通过REST api更新页面的想法/建议?还是我完全错了?
Update:
在@SallyCJ的出色帮助下,这是我最终得到的东西的最终形式,以防它能帮助其他人:
<小时>
import base64, json, re, requests
user = \'admin\'
pythonapp = \'QUDM PYFn xfyK 9Hhe Ayf2 hk6k\'
url = \'https://example.com/wp-json/wp/v2\'
data_string = user + \':\' + pythonapp
token = base64.b64encode(data_string.encode())
headers = {\'Authorization\':\'Basic \' + token.decode(\'utf8\')}
# Temporary URL for the sake of testing
newsletterURL = \'http://www.example.com/wp-content/uploads/2019/08/lorem-ipsum.pdf\'
# Download the current Newsletter page
r = requests.get(url + \'/pages/6\', headers=headers)
# Extract the actual page content from all the rest of the API data
data = r.json()
content = data[\'content\'][\'rendered\']
# Find the URL for the download in \'content\', and replace it with the new URL
regex = r"(http(s)?)://(www.)?(example.com\\/wp-content/uploads\\/).+(pdf)"
newContent = re.sub(regex, newsletterURL,content)
# Create a new data payload using the updated \'content\'
data = {"content": newContent}
# Upload it back to the site
r = requests.post(url + \'/pages/6\', headers=headers, json=data)
最合适的回答,由SO网友:Sally CJ 整理而成
The problem is with your POST
request, where the content
property should be a plain string 而不是一个物体。You should also send just the data that you want to change.
更新页面时(例如,类型为page
), 您应该提供一个JSON编码的或URL编码的字符串,其中包含一个或多个列出的属性/参数here.
使用的示例cURL 从命令行提示:
URL编码(Content-Type
标题为application/x-www-form-urlencoded
):
curl -d "title=New%20Title!&content=New%20Content%3F" -X POST https://example.com/wp-json/wp/v2/pages/9 --user "admin:QUDM PYFn xfyK 9Hhe Ayf2 hk6k"
JSON编码
curl -H "Content-Type: application/json" -d "{\\"title\\":\\"New Title!\\",\\"content\\":\\"New Content?\\"}" -X POST https://example.com/wp-json/wp/v2/pages/9 --user "admin:QUDM PYFn xfyK 9Hhe Ayf2 hk6k"
因此,在您的代码中:
这一行:
tmp[\'content\'][\'rendered\'] = newPageContent
应为:
tmp = { \'content\': newPageContent }
但您当然可以添加其他要更改的属性;e、 g。
title
和
status
.
尽管我不是Python专家,但在创建/检索/更新/删除页面时,您可以使用以下示例作为参考:
import base64, json, requests
user = \'admin\'
pythonapp = \'QUDM PYFn xfyK 9Hhe Ayf2 hk6k\'
url = \'https://example.com/wp-json/wp/v2\'
data_string = user + \':\' + pythonapp
token = base64.b64encode( data_string.encode() )
headers = { \'Authorization\': \'Basic \' + token.decode( \'utf8\' ) }
# Create a Page.
data = { \'title\': \'Testing from Python\' }
r = requests.post( url + \'/pages/\', headers=headers, json=data )
data = r.json()
page_id = str( data[\'id\'] )
print \'Page created. ID: \' + page_id
# Retrieve the Page.
r = requests.get( url + \'/pages/\' + page_id, headers=headers )
data = r.json()
title = data[\'title\'][\'rendered\']
print \'Page #\' + page_id + \' retrieved. The title is: \' + title
# Update the Page.
data = { \'content\': \'New content here\' }
r = requests.post( url + \'/pages/\' + page_id, headers=headers, json=data )
data = r.json()
content = data[\'content\'][\'rendered\']
print \'Page #\' + page_id + \' updated. Content set to: \' + content
# Delete the Page.
r = requests.delete( url + \'/pages/\' + page_id, headers=headers )
data = r.json()
print \'Page #\' + page_id + \' moved to the Trash. (Well, it should be...)\'
print \'The Page "status" is \' + data[\'status\']