pip3 install requests
哦吼,pip太老了要先更新
问题不大更新他一手
python -m pip install --upgrade pip
搞定再来安装,这下成功了
验证一下
pip list
可以看到已安装了request包
from urllib import request url = r"http://www.baidu.com" req = request.Request(url) # 构造request请求 response = request.urlopen(req).read().decode() # 获取response响应 print(response)
好的执行成功返回了一个html页面
from urllib import request import re headers = { "User-Agent": "Mozilla/5.0" } url = r"http://www.baidu.com" req = request.Request(url, headers=headers) # 构造请求 response = request.urlopen(req).read().decode() # 获取响应 pat = r"<title>(.*?)</title>" # 正则匹配规则 data = re.findall(pat, response) # 正则匹配筛选 print(data)