为什么urllib2.urlopen打不开“http://localhost/new-post#comment-29”这样的页面?

发布于 2024-09-25 03:31:05 字数 294 浏览 1 评论 0原文

我很好奇,为什么运行此行时会出现 404 错误:

urllib2.urlopen("http://localhost/new-post#comment-29")

虽然冲浪一切正常 http://localhost /new-post#comment-29 在任何浏览器中...

urlopen 方法不解析其中包含“#”的网址?

有人知道吗?

I'm curious, how come I get 404 error running this line:

urllib2.urlopen("http://localhost/new-post#comment-29")

While everything works fine surfing http://localhost/new-post#comment-29 in any browser...

urlopen method does not parse urls with "#" in it?

Anybody knows?

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

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

发布评论

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

评论(1

马蹄踏│碎落叶 2024-10-02 03:31:05

在 HTTP 协议中,片段(从 # 开始)不会通过网络发送到服务器:它由浏览器在本地保留并使用,一旦服务器的响应被完全接收到,以某种方式“视觉上”定位”页面中要显示为“当前”的确切位置(例如,如果返回的页面是 HTML 格式,则这将通过解析 HTML 并查找第一个合适的 标志)。

因此,过程是:例如通过 urlparse.urlparse 删除片段;使用剩余的部分来获取资源;根据服务器响应的内容类型标头适当地解析它;然后,基于在解析的资源中定位您在第一步中保留的片段,采取程序对资源上的“当前位置”执行的任何视觉操作。

In the HTTP protocol, the fragment (from # onwards) is not sent to the server across the network: it's locally retained by the browser and used, once the server's response is fully received, to somehow "visually locate" the exact spot in the page to be shown as "current" (for example, if the returned page is in HTML, this will be done by parsing the HTML and looking for the first suitable <a> flag).

So, the procedure is: remove the fragment e.g. via urlparse.urlparse; use the rest to fetch the resource; parse it appropriately based on the server response's content-type header; then take whatever visual action your program does regarding the "current spot" on the resource, based on locating within the parsed resource the fragment you retained in the first step.

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