from urllib import parse url = 'https://book.qidian.com/info/1004608738?wd=123&page=20#Catalog' """ url:待解析的url scheme='':假如解析的url没有协议,可以设置默认的协议,如果url有协议,设置此参数无效 allow_fragments=True:是否忽略锚点,默认为True表示不忽略,为False表示忽略 """ result = parse.urlparse(url=url, scheme='http', allow_fragments=True) print(result) print(result.scheme) # 协议 print(result.netloc) # 域名 print(result.path) # 路径 print(result.params) # 参数 print(result.query) # 查询条件,一般都是get请求 print(result.fragment) # 锚点,用于直接定位页面的下拉位置 """ https book.qidian.com /info/1004608738 "" wd=123&page=20 Catalog """ # urlunparse()可以实现URL的构造 url_parmas = ('https', 'book.qidian.com', '/info/1004608738', '', 'wd=123&page=20', 'Catalog') url_result = parse.urlunparse(url_parmas) print(url_result) """ https://book.qidian.com/info/1004608738?wd=123&page=20#Catalog """ # urljoin()传递一个基础链接,根据基础链接可以将某一个不完整的链接拼接为一个完整链接 base_url = 'https://book.qidian.com/info/1004608738?wd=123&page=20#Catalog' path_sub = '/info/100861102' full_url = parse.urljoin(base_url, path_sub) print(full_url) """ https://book.qidian.com/info/100861102 """ # urlencode()将字典构形式的参数序列化为url编码后的字符串(常用来构造get请求和post请求的参数)k1=v1&k2=v2 parmas = { 'wd':'123', 'page':20 } parmas_str = parse.urlencode(parmas) print(parmas_str) """ page=20&wd=123 """ # parse_qs() 将url编码格式的参数反序列化为字典类型 parmas_str = 'page=20&wd=123' parmas = parse.parse_qs(parmas_str) print(parmas) """ {'page': ['20'], 'wd': ['123']} """ # quote()可以将中文转换为URL编码格式 word = '中国梦' url = 'http://www.baidu.com/s?wd='+parse.quote(word) print(parse.quote(word)) print(url) """ %E4%B8%AD%E5%9B%BD%E6%A2%A6 http://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD%E6%A2%A6 """ # unquote:可以将URL编码进行解码 url = 'http://www.baidu.com/s?wd=%E4%B8%AD%E5%9B%BD%E6%A2%A6' print(parse.unquote(url)) """ http://www.baidu.com/s?wd=中国梦 """
转载:https://www.cnblogs.com/wuzm/p/11655035.html