簡體   English   中英

在 Python 中 HTTP GET 的最快方法是什么?

[英]What is the quickest way to HTTP GET in Python?

如果我知道內容將是一個字符串,那么在 Python 中 HTTP GET 的最快方法是什么? 我正在搜索文檔中的快速單行文件,例如:

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

但是我可以使用 Google 找到的只有httpliburllib - 我無法在這些庫中找到快捷方式。

標准 Python 2.5 是否具有上述某種形式的快捷方式,還是應該編寫函數url_get

  1. 我不想捕獲炮擊的輸出wgetcurl

蟒蛇3:

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

蟒蛇2:

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

urllib.requestread文檔。

您可以使用名為requests的庫。

import requests
r = requests.get("http://example.com/foo/bar")

這很容易。 然后你可以這樣做:

>>> print(r.status_code)
>>> print(r.headers)
>>> print(r.content)

如果您希望使用 httplib2 的解決方案成為 oneliner,請考慮實例化匿名 Http 對象

import httplib2
resp, content = httplib2.Http().request("http://example.com/foo/bar")

看看httplib2 ,它 - 除了許多非常有用的功能 - 提供了你想要的。

import httplib2

resp, content = httplib2.Http().request("http://example.com/foo/bar")

其中 content 將是響應正文(作為字符串),而 resp 將包含狀態和響應標頭。

雖然它不包含在標准 python 安裝中(但它只需要標准 python),但絕對值得一試。

使用強大的urllib3庫就足夠簡單了。

像這樣導入:

import urllib3

http = urllib3.PoolManager()

並提出這樣的請求:

response = http.request('GET', 'https://example.com')

print(response.data) # Raw data.
print(response.data.decode('utf-8')) # Text.
print(response.status) # Status code.
print(response.headers['Content-Type']) # Content type.

您也可以添加標題:

response = http.request('GET', 'https://example.com', headers={
    'key1': 'value1',
    'key2': 'value2'
})

更多信息可以在urllib3 文檔中找到。

urllib3比內置的urllib.requesthttp模塊更安全、更容易使用並且穩定。

無需進一步必要的導入,此解決方案(對我而言)有效 - 也適用於 https:

try:
    import urllib2 as urlreq # Python 2.x
except:
    import urllib.request as urlreq # Python 3.x
req = urlreq.Request("http://example.com/foo/bar")
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36')
urlreq.urlopen(req).read()

在標題信息中未指定“用戶代理”時,我經常難以抓取內容。 然后通常會使用以下內容取消請求: urllib2.HTTPError: HTTP Error 403: Forbiddenurllib.error.HTTPError: HTTP Error 403: Forbidden

theller 的 wget 解決方案非常有用,但是,我發現它不會在整個下載過程中打印出進度。 如果在 reporthook 中的打印語句后添加一行就完美了。

import sys, urllib

def reporthook(a, b, c):
    print "% 3.1f%% of %d bytes\r" % (min(100, float(a * b) / c * 100), c),
    sys.stdout.flush()
for url in sys.argv[1:]:
    i = url.rfind("/")
    file = url[i+1:]
    print url, "->", file
    urllib.urlretrieve(url, file, reporthook)
print

如何也發送標題

蟒蛇3:

import urllib.request
contents = urllib.request.urlopen(urllib.request.Request(
    "https://api.github.com/repos/cirosantilli/linux-kernel-module-cheat/releases/latest",
    headers={"Accept" : 'application/vnd.github.full+json"text/html'}
)).read()
print(contents)

蟒蛇2:

import urllib2
contents = urllib2.urlopen(urllib2.Request(
    "https://api.github.com",
    headers={"Accept" : 'application/vnd.github.full+json"text/html'}
)).read()
print(contents)

這是一個用 Python 編寫的 wget 腳本:

# From python cookbook, 2nd edition, page 487
import sys, urllib

def reporthook(a, b, c):
    print "% 3.1f%% of %d bytes\r" % (min(100, float(a * b) / c * 100), c),
for url in sys.argv[1:]:
    i = url.rfind("/")
    file = url[i+1:]
    print url, "->", file
    urllib.urlretrieve(url, file, reporthook)
print

實際上,在 Python 中,我們可以像從文件一樣讀取 HTTP 響應,這里有一個從 API 讀取 JSON 的示例。

import json
from urllib.request import urlopen

with urlopen(url) as f:
    resp = json.load(f)

return resp['some_key']

如果您專門使用 HTTP API,還有更方便的選擇,例如Nap

例如,以下是從20145 月 1 日起從 Github 獲取要點的方法:

from nap.url import Url
api = Url('https://api.github.com')

gists = api.join('gists')
response = gists.get(params={'since': '2014-05-01T00:00:00Z'})
print(response.json())

更多示例: https : //github.com/kimmobrunfeldt/nap#examples

優解軒,塞勒。

為了它與python 3一起使用,請進行以下更改

import sys, urllib.request

def reporthook(a, b, c):
    print ("% 3.1f%% of %d bytes\r" % (min(100, float(a * b) / c * 100), c))
    sys.stdout.flush()
for url in sys.argv[1:]:
    i = url.rfind("/")
    file = url[i+1:]
    print (url, "->", file)
    urllib.request.urlretrieve(url, file, reporthook)
print

此外,您輸入的 URL 應以“http://”開頭,否則返回未知 url 類型錯誤。

對於python >= 3.6 ,您可以使用dload

import dload
t = dload.text(url)

對於json

j = dload.json(url)

安裝:
pip install dload

如果你想要一個較低級別的 API:

import http.client

conn = http.client.HTTPSConnection('example.com')
conn.request('GET', '/')

resp = conn.getresponse()
content = resp.read()

conn.close()

text = content.decode('utf-8')

print(text)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM