Python数据获取——爬虫(1)
1. 利用Request()方法
req = urllib.request.Request(url,headers)构建请求对象
res = urllib.request.urllopen(req)构建响应对象
html = res.read().decode(“utf-8”)获取内容
import urllib.request
import urllib.parse
headers = {"User-Agent":"Mozilla/5.0 "}
baseurl = "http://www.baidu.com/s?wd="
key = input('请输入要查询的内容')
url = baseurl + urllib.parse.quote(key)
req = urllib.request.Request(url,headers=headers)
res= urllib.request.urlopen(req)
html = res.read().decode("utf-8")
print(html)