Python 中 Urllib 的意外行为

发布于 2024-12-18 04:39:21 字数 332 浏览 3 评论 0原文

我的系统不支持任何代理。

params = urllib.urlencode({'search':"August Rush"})
f = urllib.urlopen("http://www.thepiratebay.org/search/query", params)

这会进入无限循环(或者只是挂起)。显然我可以摆脱这个并使用 FancyUrlOpener 并自己创建查询而不是传递参数。但是,我认为按照我现在的方式做是一种更好、更干净的方法。

编辑:这更多的是一个网络问题,其中我的 Ubuntu 工作站配置为不同的代理。必须做一些改变并且它起作用了。谢谢你!

My system is not behind any proxy.

params = urllib.urlencode({'search':"August Rush"})
f = urllib.urlopen("http://www.thepiratebay.org/search/query", params)

This goes onto an infinite loop(Or just hangs). I can obviously get rid of this and use FancyUrlOpener and create the query myself rather than passing it parameters. But, I think doing the way I'm doing now is a better and cleaner approach.

Edit: This was more of a networking problem in which my Ubuntu workstation was configured to a different proxy. Had to do certain changes and it worked. Thank you!

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

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

发布评论

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

评论(2

獨角戲 2024-12-25 04:39:21

发布的代码对我来说工作得很好,在 Windows 上使用 Python 2.7.2。

您是否尝试过使用 http 调试工具,例如 Fiddler2 来查看您的程序与网站?

如果您在本地主机的端口 8888 上运行 Fiddler2,您可以执行以下操作来查看请求和响应:

import urllib
proxies = {"http": "http://localhost:8888"}
params = urllib.urlencode({'search':"August Rush"})
f = urllib.urlopen("http://www.thepiratebay.org/search/query", params, proxies)
print len(f.read())

The posted code works fine for me, with Python 2.7.2 on Windows.

Have you tried using a http-debugging tool, like Fiddler2 to see the actual conversation going between your program and the site?

If you run Fiddler2 on port 8888 on localhost, you can do this to see the request and response:

import urllib
proxies = {"http": "http://localhost:8888"}
params = urllib.urlencode({'search':"August Rush"})
f = urllib.urlopen("http://www.thepiratebay.org/search/query", params, proxies)
print len(f.read())
末が日狂欢 2024-12-25 04:39:21

这对我有用:

import urllib

params = urllib.urlencode({'q': "August Rush", 'page': '0', 'orderby': '99'})
f = urllib.urlopen("http://www.thepiratebay.org/s/", params)

with open('text.html', 'w') as ff:
    ff.write('\n'.join(f.readlines()))

我使用启用了网络检查器的 Google Chrome 打开 http://www.thepiratebay.org 。我将“August Rush”输入搜索字段,然后按“搜索”。然后我分析了发送的标头并执行了上面的代码。

This works for me:

import urllib

params = urllib.urlencode({'q': "August Rush", 'page': '0', 'orderby': '99'})
f = urllib.urlopen("http://www.thepiratebay.org/s/", params)

with open('text.html', 'w') as ff:
    ff.write('\n'.join(f.readlines()))

I opened http://www.thepiratebay.org with Google Chrome with network inspector enabled. I put "August Rush" into the search field and pressed 'Search'. Then i analyzed the headers sent and did the code above.

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