在Python中使用HTTP GET的最快方法是什么? [英] What is the quickest way to HTTP GET in Python?

查看:241
本文介绍了在Python中使用HTTP GET的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我知道内容将是一个字符串,那么在Python中使用HTTP GET的最快方法是什么?我正在搜索文档中的快速单行,如:

What is the quickest way to HTTP GET in Python if I know the content will be a string? I am searching the docs for a quick one-liner like:

contents = url.get("http://example.com/foo/bar")

但我能用谷歌找到的只有 httplib urllib - 我无法在这些库中找到快捷方式。

But all I can find using Google are httplib and urllib - and I am unable to find a shortcut in those libraries.

标准Python 2.5是否有如上所述的某种形式的快捷方式,或者我应该写一个函数 url_get

Does standard Python 2.5 have a shortcut in some form as above, or should I write a function url_get?


  1. 我不希望将shell的输出捕获到 wget curl

  1. I would prefer not to capture the output of shelling out to wget or curl.


推荐答案

Python 2.x:

Python 2.x:

import urllib2
contents = urllib2.urlopen("http://example.com/foo/bar").read()

Python 3.x:

Python 3.x:

import urllib.request
contents = urllib.request.urlopen("http://example.com/foo/bar").read()

urllib.request 读取

这是怎么回事?

这篇关于在Python中使用HTTP GET的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆