urllib.request和urllib.parse模块

常用的方法

urllib.requests.ulopen(“网址”):向网站发起一个请求并获取响应

from urllib import request

resp = request.urlopen("http://www.baidu.com")
print(resp.geturl())# 具体请求的网址
print(resp.getcode())
print(resp.read(10))# 读取10字节
print(resp.readline())#读取第一行
print(resp.readlines())# 读取每一行,列表返回
  1. 下载图片或其他资源:
from urllib import request
lady_url="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTEhMVFhUXFxYYGBgWFhcXGBUVFRUWFxUVFhgYHSggGBolHRcVITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0lHyUtLSstLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAO4A0wMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAFAgMEBgcBAAj/xABGEAABAwIDBAYHBQYFAgcAAAABAAIRAwQSITEFQVGRBhMiYXGBMkJSobHB0QcUI2LwFXKCkrLhJDNTovFD0hY0Y4OzwvL/xAAZAQACAwEAAAAAAAAAAAAAAAABAwACBAX/xAAkEQACAgICAwEBAQADAAAAAAAAAQIRAxIhMQRBUSITYTJScf/aAAwDAQACEQMRAD8A0K12ZVqZl+m8yUbt9nkAS+fJTGYdAnEU2BdDdOiB3pi8ZkfBSQSk125FRMj5RW7Q9o+C7sSu1tWs0kCXGJMb5+aYtH9s+aRXbSZjqOMSZcZynQR7k2hNlpovGfabzCeGHuWW3vSkNd+GyRnqczyyCZqdOqzAB1LWD2iCZ7vequAxNms9YOI5rhqt9ocws2sOnJPpMxcewByhx+CtOytrsuB+GQD7JaJ/ug40Wth0VGe03mF37wz228whrmVO7+Vv0SMNTiP5W/RCiuwT+8U/abzXvvdP2ghnV1OPub9F3qantfBSibMI/fafte4/Re+/U/a9x+iHig/2jzXRQf7TuZUpEtk777T4nkfomL28aabwJJLSBkd4hNi3d7TuZXTaniealEtgDepOwv8AOb4O+BT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D3kx3KtVapaAJzOZP0SqNd5Ig6AxwAAlN2SFaNmhW+12gBjmsLjmcQbmeEfrRM1dv0mn0acmcwxuUb9FRuuI7XkmXXZnRT8gqRf39LnQIk5+QA4Lr+ldT1feZWe/fSlNvXIfj4HWf00X/xTUjVoOXEp0dIzGYYT3hZt99cnPv7kPx8IlP6aN+36e+lTJ8F5Z0NoOXkNMfwvtk+n/9k="
request.urlretrieve(lady_url,"lady.jpg")

urllib.parse

  1. urlencode 将字典进行url编码
from urllib import parse

url="https://www.baidu.com/s"
params={"wd":"刘德华"}
qs=parse.urlencode(params)
url=url+"?"+qs
print(url)
  1. parse_qs urlencode的逆
from urllib import parse
params={"wd":"刘德华"}
qs=parse.urlencode(params)
print(parse.parse_qs(qs))

3.urlparse urlsplit,解析url

url="https://www.baidu.com/s/w?wd=python&username=abd#2"
result=parse.urlparse(url)
print(result)
print(result.fragment)
print(result.path)
result=parse.urlsplit(url)
print(result)
打印结果:
ParseResult(scheme='https', netloc='www.baidu.com', path='/s/w', params='', query='wd=python&username=abd', fragment='2')
2
/s/w
SplitResult(scheme='https', netloc='www.baidu.com', path='/s/w', query='wd=python&username=abd', fragment='2')

区别是:只有一个,urlparse会多解析一个params

你可能感兴趣的:(urllib.request和urllib.parse模块)