如何使用Python显示网页?

发布于 2024-12-19 09:01:05 字数 249 浏览 5 评论 0原文

我想读取 Python 中的用户输入以获取网址(例如 http://www.google.com)然后以 HTML 格式(仅限文本)将网页打印到终端。我尝试使用 pexpect.spawn('elinks') 但 elinks 似乎没有写入标准输出。我还查看了 HTMLParser 模块,但我不知道如何将生成的文本格式化为类似于网页的内容。有什么建议吗?

I want to read user input in Python to get a url (e.g. http://www.google.com) and then print the web page in HTML formatting (text only) to the terminal. I tried using pexpect.spawn('elinks') but elinks doesn't seem to write to stdout. I also looked at the HTMLParser module, but I don't know how I format the resulting text into something resembling a webpage. Any advice?

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

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

发布评论

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

评论(3

゛清羽墨安 2024-12-26 09:01:05

这是一个不小的挑战。事实上,您想要生成elinks,这让我想知道为什么您不直接使用它。查看它具有哪些可扩展性/插件/附加选项,或者尝试重写它以满足您的特定需求。

最终,您需要使用curses后端编写自己的浏览器布局引擎。如果您使用Python,urwid是curses布局的流行选择。

This is no small challenge. The fact that you want to spawn elinks makes me wonder why you don't just use it instead. See what extensibility/plugin/addon options it has, or try rewriting it to suit your specific needs.

Ultimately, you'll need to write your own browser layout engine with a curses backend. If you're using python, urwid is a popular choice for curses layouts.

爱已欠费 2024-12-26 09:01:05

使用 python urllib

输入 url --> urllib -->;该页面-->在控制台中打印

# example in the python urllib page

import urllib

opener = urllib.FancyURLopener({})
f = opener.open("http://www.python.org/")
f.read()

# modify:

html = f.read()

# add:

print html

# to print in terminal

类似于unix中的“curl”

with python urllib

enter the url --> urllib --> the page --> print in console

# example in the python urllib page

import urllib

opener = urllib.FancyURLopener({})
f = opener.open("http://www.python.org/")
f.read()

# modify:

html = f.read()

# add:

print html

# to print in terminal

its similar to "curl" in unix

调妓 2024-12-26 09:01:05
import requests
r = requests.get('http://www.google.com/')
print(r.content)
import requests
r = requests.get('http://www.google.com/')
print(r.content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文