如何打印到JSON文件。打印到终端,打开JSON。无数据传输

发布于 2025-02-11 05:20:41 字数 1136 浏览 2 评论 0原文

您如何解决将此输出打印到结构化的JSON?

此代码创建JSON文件,并在我的终端中打印响应。但不会将结构化数据发送到JSON文件。

from seoanalyzer import analyze
from bs4 import BeautifulSoup
site = 'http://www.microblink.com'

output = analyze(site, follow_links=False)

with open("output.json", "w", encoding = 'utf-8') as file:

    print(output)

// 样本输出:

{'pages': [{'url': 'http://www.microblink.com', 'title': 'microblink: ai-driven technology for solving real life problems', 'description': 'create amazing experiences, verify users at scale and automate your document-based workflow with ai tech built for a remote world.', 'word_count': 659, 'keywords': [(17, 'identity'), (13, 'verification'), (9, 'microblink'), (8, 'technology'), (8, 'industries'), (7, 'contact'), (6, 'document'), (6, 'blog'), (5, 'resources'), (5, 'experiences'), (5, 'customers')], 'bigrams': Counter({'identity verification': 10, 'sign up': 3, 'your customers': 3, 'verification in': 3, 'able to': 3, 'we re': 3, 'identity identity': 2, 'verification document': 2, 'document verification': 2, 'verification identity': 2, 'identity document': 2, 

How do you solve for getting this output to print to a structured json?

This code creates the json file, and prints the response in my terminal. But does not send the structured data to the json file.

from seoanalyzer import analyze
from bs4 import BeautifulSoup
site = 'http://www.microblink.com'

output = analyze(site, follow_links=False)

with open("output.json", "w", encoding = 'utf-8') as file:

    print(output)

//
Sample output:

{'pages': [{'url': 'http://www.microblink.com', 'title': 'microblink: ai-driven technology for solving real life problems', 'description': 'create amazing experiences, verify users at scale and automate your document-based workflow with ai tech built for a remote world.', 'word_count': 659, 'keywords': [(17, 'identity'), (13, 'verification'), (9, 'microblink'), (8, 'technology'), (8, 'industries'), (7, 'contact'), (6, 'document'), (6, 'blog'), (5, 'resources'), (5, 'experiences'), (5, 'customers')], 'bigrams': Counter({'identity verification': 10, 'sign up': 3, 'your customers': 3, 'verification in': 3, 'able to': 3, 'we re': 3, 'identity identity': 2, 'verification document': 2, 'document verification': 2, 'verification identity': 2, 'identity document': 2, 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-02-18 05:20:41

您刚刚用打开了JSON文件,并带有打开(“ output.json”,“ w”,encoding =“ utf-8”)作为文件而无需写任何内容。

要将数据保存到JSON文件中,您需要将数据转储到JSON文件中。

with open("output.json", "w", encoding = 'utf-8') as file:

    print(output)
    json.dump(output, file)

为此,您需要在代码中导入JSON

You have just opened the json file with with open("output.json", "w", encoding = "utf-8") as file without writing anything to it.

To save your data to the json file you need to dump the data into the json file.

with open("output.json", "w", encoding = 'utf-8') as file:

    print(output)
    json.dump(output, file)

For that to work you need to import json in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文