有没有一种方法可以使用 urllib 打开一个站点,直到其中出现指定的对象?

发布于 2024-12-19 19:02:02 字数 95 浏览 2 评论 0原文

我正在使用 urllib 打开一个网站并获取有关该网站的一些信息。

有没有办法仅“打开”此网站我需要的部分并丢弃其余部分(丢弃我的意思是不打开/加载其余部分)?

I'm using urllib to open one site and get some information on it.

Is there a way to "open" this site only to the part I need and discard the rest (discard I mean don't open/load the rest)?

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

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

发布评论

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

评论(2

云醉月微眠 2024-12-26 19:02:02

我不确定你想做什么。如果您只是尝试解析网站以查找有用的“信息”,那么我建议使用库 BeautifulSoup 。该库可以轻松保留网站的某些部分,同时丢弃其余部分。

但是,如果您试图通过仅下载网站的一部分来节省下载带宽,那么您将需要做更多的工作。如果是这种情况,请在您的问题中说明,我将更新答案。

I'm not sure what you are trying to do. If you are simply trying to parse the site to find the useful "information", then I recommend using the library BeautifulSoup. That library makes it easy to keep certain parts of the site while discarding the rest.

If however you trying to save download bandwidth by downloading only a piece of the site, then you will need to do a lot more work. If that is the case please say so in your question and I'll update the answer.

与酒说心事 2024-12-26 19:02:02

您应该能够读取(字节)而不是读取(),这将读取多个字节而不是全部。然后附加到已下载的字节,并查看它是否包含您要查找的内容。然后你应该能够使用 .close() 停止下载。

You should be able to read(bytes) instead of read(), this will read a number of bytes instead of all of it. Then append to already downloaded bytes, and see if it contains what you're looking for. Then you should be able to stop download with .close().

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