// An highlighted block
import urllib.request # 加载模块
try:
url = "http://www.douban.com"
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.42 Safari/537.36 Edg/86.0.622.19"
}
req = urllib.request.Request(url=url, headers=headers) # req=request封装请求信息
response = urllib.request.urlopen(req) # 传入封装的req请求信息,再获取响应
# response = urllib.request.urlopen(url) # 直接传入url会报418 ERROR,被识别python爬虫,所以要修改请求信息
print(response.read().decode("utf-8")) # 先读取响应内容,再解码
except Exception as ex: # 捕获异常
print(f"{ex} 报错了!")
urllib模块,案例:豆瓣网请求设置及详细说明
最新推荐文章于 2021-08-09 17:05:14 发布