URL WWW-ң қ ғ ө.
import urllib
doc = urllib.urlopen("http://python.onego.ru").read()
print doc[:40]
urllib.urlopen() read() ә қ, ә құ. ң қ ә: readline(), readlines(), fileno(), close() ә ұ , -қ Message- ғ ә қ info() ә . ұ қ ә ү қғ :
>>> import urllib
>>> f = urllib.urlopen("http://python.onego.ru")
>>> print f.info()
Date: Sat, 25 Dec 2004 19:46:11 GMT
Server: Apache/1.3.29 (Unix) PHP/4.3.10
Content-Type: text/html; charset=windows-1251
Content-Length: 4291
>>> print f.info()['Content-Type']
text/html; charset=windows-1251
urllib.urlopen() ө ү , , web- ә . ғ, web-ң ғ ә web- GET ә POST ә қ ү. GET ә URL- "?" қ ң , POST әң ғ ә HTTP-ұң . Ƴң ұқ ө :
import urllib
data = {"search": "Python"}
enc_data = urllib.urlencode(data)
# GET
f = urllib.urlopen("http://searchengine.com/search" + "?" + enc_data)
print f.read()
# POST
f = urllib.urlopen("http://searchengine.com/search", enc_data)
print f.read()
ғ ә қ ғ . ұ ғ urllib.urlencode() ө -ә ұң қғ :
>>> import urllib
>>> data = [("n", "1"), ("n", "3"), ("n", "4"), ("button", ""),]
>>> enc_data = urllib.urlencode(data)
>>> print enc_data
n=1&n=3&n=4&button=%F0%D2%C9%D7%C5%D4
urllib - қ - ү ү . ө,қ ә қ - қ. Unix- - http_proxy, ftp_proxy ә .. . Windows- - ғ, Mac OS- Internet . - proxies urllib.urlopen() қ .
|
|
#
proxies = proxies={'http': 'http://www.proxy.com:3128'}
f = urllib.urlopen(some_url, proxies=proxies)
#
f = urllib.urlopen(some_url, proxies={})
#
f = urllib.urlopen(some_url, proxies=None)
f = urllib.urlopen(some_url)
urlretrieve() URL ғ ү . :
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
ұғ url- ң URL-, filename- ү , reporthook- ү ү ү қ , data-POST ( қ) ә ү ә. (filepath, headers) қ, ұғ filepath .
urllib.urlretrieve() ң қ ү reporthook() қ ө қ. ұ ү :қғ , ө, ң ә ғ (ұ -1- ң, ) қғ ө.
ғ ү қ.
FILE = 'boost-1.31.0-9.src.rpm'
URL = 'http://download.fedora.redhat.com/pub/fedora/linux/core/3/SRPMS/' + FILE
def download(url, file):
import urllib, time
start_t = time.time()
def progress(bl, blsize, size):
dldsize = min(bl*blsize, size)
if size!= -1:
p = float(dldsize) / size
try:
elapsed = time.time() - start_t
est_t = elapsed / p - elapsed
except:
est_t = 0
print "%6.2f %% %6.0f s %6.0f s %6i / %-6i bytes" % (
p*100, elapsed, est_t, dldsize, size)
else:
print "%6i / %-6i bytes" % (dldsize, size)
urllib.urlretrieve(URL, FILE, progress)
download(URL, FILE)
ұ ғ ғ
0.00 % 1 s 0 s 0 / 6952309 bytes
0.12 % 5 s 3941 s 8192 / 6952309 bytes
0.24 % 7 s 3132 s 16384 / 6952309 bytes
0.35 % 10 s 2864 s 24576 / 6952309 bytes
0.47 % 12 s 2631 s 32768 / 6952309 bytes
0.59 % 15 s 2570 s 40960 / 6952309 bytes
0.71 % 18 s 2526 s 49152 / 6952309 bytes
0.82 % 20 s 2441 s 57344 / 6952309 bytes