如何在不使用浏览器的情况下使用 Python 打开 URL?

发布于 2024-12-22 19:22:59 字数 597 浏览 0 评论 0原文

我想用 Python 代码打开 URL,但不想使用“webbrowser”模块。我已经尝试过并且它有效(它在我实际的默认浏览器中打开了 URL,这是我不想要的)。于是我尝试使用 urllib (urlopen) 和 mechanize。他们都对我的程序运行良好,但他们都没有真正将我的请求发送到网站!

这是我的代码的一部分:

finalURL="http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=" + str(newPID) + "&xxx_c_1_f_987=" + str(ZA[z])

print finalURL

print ""

br.open(finalURL)

page = urllib2.urlopen(finalURL).read()

当我进入网站 locationary.com 时,它没有显示已进行任何更改!不过,当我使用“webbrowser”时,在我提交 URL 后,它确实在网站上显示了更改。如何在不实际打开浏览器的情况下完成与网络浏览器相同的操作?

我认为该网站想要一个“GET”

I want to open a URL with Python code but I don't want to use the "webbrowser" module. I tried that already and it worked (It opened the URL in my actual default browser, which is what I DON'T want). So then I tried using urllib (urlopen) and mechanize. Both of them ran fine with my program but neither of them actually sent my request to the website!

Here is part of my code:

finalURL="http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=" + str(newPID) + "&xxx_c_1_f_987=" + str(ZA[z])

print finalURL

print ""

br.open(finalURL)

page = urllib2.urlopen(finalURL).read()

When I go into the site, locationary.com, it doesn't show that any changes have been made! When I used "webbrowser" though, it did show changes on the website after I submitted my URL. How can I do the same thing that webbrowser does without actually opening a browser?

I think the website wants a "GET"

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

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

发布评论

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

评论(2

戏舞 2024-12-29 19:22:59

我不确定您正在使用什么操作系统,但是如果您使用 httpscoop (mac) 或 fiddler ( pc)或wireshark,您应该能够观察流量并了解发生了什么。网站可能会执行重定向(您的浏览器正在跟踪)或有其他一些后续活动。

启动 HTTP 嗅探器,使用 Web 浏览器发出请求并观察流量。完成此操作后,使用 python 脚本尝试一下,看看是否发出了请求,以及 HTTP 流量有何不同。这应该有助于确定断开连接的位置。

I'm not sure what OS you're working on, but if you use something like httpscoop (mac) or fiddler (pc) or wireshark, you should be able to watch the traffic and see what's happening. It may be that the website does a redirect (which your browser is following) or there's some other subsequent activity.

Start an HTTP sniffer, make the request using the web browser and watch the traffic. Once you've done that, try it with the python script and see if the request is being made, and what the difference is in the HTTP traffic. This should help identify where the disconnect is.

不美如何 2024-12-29 19:22:59

HTTP GET 不需要客户端的任何特定代码或操作:它只是基本 URL (http://server/) + 路径 + 可选查询。

如果 URL 正确,那么上面的代码应该可以工作。接下来您可以尝试的一些提示:

  1. URL 真的正确吗?使用 Firebug 或类似工具来观察网络流量,它会为您提供完整的 URL 以及 HTTP 请求中的任何标头字段。

  2. 也许该网站要求您首先登录。如果是这样,请确保您正确设置 cookie。

  3. 某些网站需要正确的“引荐来源网址”字段(以保护自己免受深层链接的影响)。添加您的浏览器在请求中使用的引用标头。

  4. 服务器的日志文件是解决此类问题的重要信息源 - 当您有权访问它时。

A HTTP GET doesn't need any specific code or action on the client side: It's just the base URL (http://server/) + path + optional query.

If the URL is correct, then the code above should work. Some pointers what you can try next:

  1. Is the URL really correct? Use Firebug or a similar tool to watch the network traffic which gives you the full URL plus any header fields from the HTTP request.

  2. Maybe the site requires you to log in, first. If so, make sure you set up cookies correctly.

  3. Some sites require a correct "referrer" field (to protect themselves against deep linking). Add the referrer header which your browser used to the request.

  4. The log file of the server is a great source of information to trouble shoot such problems - when you have access to it.

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