(1) 403 Forbidden
我想获得知乎某个问题下答主的头像,请求后响应为403 Forbidden。
解决方法来源于 https://blog.csdn.net/u011808673/article/details/80609221
第一种方式: 增加 headers成功解决问题
import re import requests from urllib import request url = 'https://www.zhihu.com/question/37180159/answer/1885390053' my_headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'} req = request.Request(url, headers=my_headers) resp = request.urlopen(req) print(resp.read().decode())