URLLIB库
import urllib.request
import urllib.parse
或
from urllib import request,parse
默认是get方法
url = 'https://www.douban.com'
headers = {'User-Agent':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request).read()
post方法
post_data = parse.urlencode([('key1','v1'),('key2','v2')])
request = request.Request('https://www.douban.com')
request.add_header('User-Agent',' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36')
response = request.urlopen(request, data=post_data.encode('utf-8')).read()
拼接url
from urllib.parse import urljoin
urljoin('https://www.douban.com','account/login?source=main')