运行结果:
代码:
import requests import base64 import json url = "https://www.xacisco.net/wp-json/wp/v2/posts?_fields=id,title,link&per_page=5&page=1" username = "xxx" password = "xxx" credentials = "{}:{}".format(username, password) token = base64.b64encode(credentials.encode()) headers = { "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36", "Authorization": "Basic {}".format(token.decode("utf-8")), "content-type": "application/json", } r=requests.get(url,headers=headers) print(json.dumps(r.json(),indent=4))