python urllib的测试

python yekong 1279℃

使用urllib读取网页 get


res = urllib.request.urlopen('https://www.wanjunshijie.com')
print(res.read().decode('utf-8'))

使用urllib post请求


# post请求
import urllib.parse

data = bytes(urllib.parse.urlencode({"hello": "word"}), encoding="utf-8")
res = urllib.request.urlopen('http://httpbin.org/post', data=data)
print(res.read().decode('utf-8'))

超时处理

try:
    res = urllib.request.urlopen('https://www.wanjunshijie.com',timeout=0.01)
    print(res.read().decode('utf-8'))
except urllib.error.URLError as e:
    print('time out')

响应头

res = urllib.request.urlopen('https://www.wanjunshijie.com')
print(res.status)
print(res.getheaders())

携带协议头

url = 'http://www.douban.com'
header = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36",
}
req = urllib.request.Request(url=url, headers=header)
req2 = urllib.request.urlopen(req)
print(req2.read().decode('utf-8'))
喜欢 (3)