# 获取一个网页的源码 https://www.baidu.com/s?wd=%E5%91%A8%E6%9D%B0%E4%BC%A6
import urllib.request
url = 'https://www.baidu.com/s?wd='
# 要将周杰伦变为Unicode编码的格式
name = urllib.parse.quote('周杰伦')
# print(name)
url = url + name
# print(url)
headers = {
'User-Agent': ''
}
# 请求对象的定制
request = urllib.request.Request(url=url, headers=headers)
# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)
# 获取响应的内容
content = response.read().decode('utf-8')
# 打印数据
print(content)
标签:10,网页,url,request,urllib,headers,源码,print
From: https://www.cnblogs.com/arciab/p/17086553.html