1. 程式人生 > >Python 自帶簡單模組使用

Python 自帶簡單模組使用

#coding:utf-8
import urllib.request
import sys
import re

googgle = urllib.request.urlopen("http://www.baidu.com")
html = googgle.read()
#獲取伺服器的表頭資訊
print("this is a header:\n%s"%googgle.info)
print ("------------------------------------------")
#返回整數狀態碼
print("this is a status:\n%s"%googgle.getcode())

print("-------------------------------------------")
#返回url
print("this is a url\n%s"%googgle.geturl())
print("-------------------------------------------")
#返回檔案描述資訊
print("this is a ex\n%s"%googgle.read().decode('utf-8'))

req = urllib.request.Request('http://python.org/')
response = urllib.request.urlopen(req)
the_page = response.read()
print(the_page)
m = re.match(r'(\w+) (\w+)(?P<sign>.*)', 'hello world! sign')
print ("m.string:", m.string)
print ("m.re:", m.re)
print ("m.pos:", m.pos)
print ("m.endpos:", m.endpos)
print ("m.lastindex:", m.lastindex)
print ("m.lastgroup:", m.lastgroup)
print ("m.group(1,2):", m.group(1, 2))
print ("m.groups():", m.groups())
print ("m.groupdict():", m.groupdict())
print ("m.start(2):", m.start(2))
print ("m.end(2):", m.end(2))
print ("m.span(2):", m.span(2))
print (r"m.expand(r'\2 \1\3'):", m.expand(r'\2 \1\3'))