以数组形式接收来自 python 的响应 - PHP

Receiving response from python as an array - PHP

我有 PHP 代码,我正在调用 python 脚本获取 URL,访问网络 URL,获取 JSON页面,然后将 JSON 页面发送回 PHP 代码,但问题是我在数组中得到了 JSON 而不是正确的 JSON 格式,anu 帮助?

python代码:

import json
import sys
import bs4
import urllib.parse
link = urllib.parse.unquote(sys.argv[1])
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36')
    page = context.new_page()
    cookie_file = open('./cookies.json')
    cookies = json.load(cookie_file)
    context.add_cookies(cookies)
    try:
        page.goto(link)
        page.wait_for_timeout(10000)
        print(page.content())
        page.close()
        context.close()
        browser.close()      
    except Exception as e:
        print("Error in playwright script.")
        page.close()
        context.close()
        browser.close()      

content 函数依赖于 document.documentElement.outerHTML,因此您可能会得到一个格式化的值。 如果请求 returns a JSON 你可以从 response 中获取文本 goto returns:

response = page.goto("https://raw.githubusercontent.com/corysimmons/colors.json/master/colors.json")
jsonContent = response.text()
jsonResult = json.loads(jsonContent)
print(jsonResult)

如果中间有一些处理,可以求:root元素的inner_text

jsonContent = page.inner_text(':root')
jsonResult = json.loads(jsonContent)
print(jsonResult)