标签:写入 例子 获取 rom 结果 odi 拼接 两种 window
import urllib.request from urllib import request, parse url = ‘http://httpbin.org/post‘ headers = { ‘User-Agent‘: ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘, ‘Host‘: ‘httpbin.org‘ } dict = { ‘name‘: ‘zhaofan‘ } data = bytes(parse.urlencode(dict), encoding=‘utf8‘) req = request.Request(url=url, data=data, headers=headers, method=‘POST‘) response = request.urlopen(req) print(response.read().decode(‘utf-8‘))
from urllib import request, parse url = ‘http://httpbin.org/post‘ dict = { ‘name‘: ‘Germey‘ } data = bytes(parse.urlencode(dict), encoding=‘utf8‘) req = request.Request(url=url, data=data, method=‘POST‘) req.add_header(‘User-Agent‘, ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘) response = request.urlopen(req) print(response.read().decode(‘utf-8‘)) #decode可以将结果格式化显示
import urllib.request proxy_handler = urllib.request.ProxyHandler({ ‘http‘: ‘http://127.0.0.1:9743‘, # 访问哪个网站格式http,https选用其中对应的一哥 ‘https‘: ‘https://127.0.0.1:9743‘ }) opener = urllib.request.build_opener(proxy_handler) response = opener.open(‘http://httpbin.org/get‘) print(response.read())
import http.cookiejar, urllib.request cookie = http.cookiejar.CookieJar() handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open(‘http://www.baidu.com‘) for item in cookie: print(item.name+"="+item.value)
import http.cookiejar, urllib.request filename = "cookie.txt" cookie = http.cookiejar.MozillaCookieJar(filename) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open(‘http://www.baidu.com‘) cookie.save(ignore_discard=True, ignore_expires=True)
import http.cookiejar, urllib.request filename = ‘cookie.txt‘ cookie = http.cookiejar.LWPCookieJar(filename) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open(‘http://www.baidu.com‘) cookie.save(ignore_discard=True, ignore_expires=True)
import http.cookiejar, urllib.request cookie = http.cookiejar.LWPCookieJar() cookie.load(‘cookie.txt‘, ignore_discard=True, ignore_expires=True) handler = urllib.request.HTTPCookieProcessor(cookie) opener = urllib.request.build_opener(handler) response = opener.open(‘http://www.baidu.com‘) print(response.read().decode(‘utf-8‘))
from urllib import request,error try: response = request.urlopen("http://pythonsite.com/1111.html") except error.URLError as e: print(e.reason)
from urllib import request,error try: response = request.urlopen("http://pythonsite.com/1111.html") except error.HTTPError as e: print(e.reason, "error.HTTPError") print(e.code, "error.HTTPError") print(e.headers, "error.HTTPError") except error.URLError as e: print(e.reason,"error.URLError") else: print("reqeust successfully")
import socket from urllib import error,request try: response = request.urlopen("http://www.pythonsite.com/",timeout=0.001) # 时间太短了 except error.URLError as e: print(type(e.reason)) if isinstance(e.reason,socket.timeout): print("time out")
from urllib.parse import urlparse result = urlparse("http://www.baidu.com/index.html;user?id=5#comment") print(result)
from urllib.parse import urlunparse data = [‘http‘,‘www.baidu.com‘,‘index.html‘,‘user‘,‘a=123‘,‘commit‘] print(urlunparse(data)) #输出http://www.baidu.com/index.html;user?a=123#commit
from urllib.parse import urljoin # print(urljoin(‘http://www.baidu.com‘, ‘FAQ.html‘)) print(urljoin(‘http://www.baidu.com‘, ‘https://pythonsite.com/FAQ.html‘)) print(urljoin(‘http://www.baidu.com/about.html‘, ‘https://pythonsite.com/FAQ.html‘)) print(urljoin(‘http://www.baidu.com/about.html‘, ‘https://pythonsite.com/FAQ.html?question=2‘)) print(urljoin(‘http://www.baidu.com?wd=abc‘, ‘https://pythonsite.com/index.php‘)) print(urljoin(‘http://www.baidu.com‘, ‘?category=2#comment‘)) print(urljoin(‘www.baidu.com‘, ‘?category=2#comment‘)) print(urljoin(‘www.baidu.com#comment‘, ‘?category=2‘))
from urllib.parse import urlencode params = { "name":"zhaofan", "age":23, } base_url = "http://www.baidu.com?" url = base_url+urlencode(params) print(url) # 输出结果为http://www.baidu.com?name=zhaofan&age=23
标签:写入 例子 获取 rom 结果 odi 拼接 两种 window
原文地址:https://www.cnblogs.com/cjj-zyj/p/10025957.html