weixin_57912041
weixin_57912041
采纳率100%
2021-05-04 20:12

pycharm调用模块时报错是怎么回事

pycharm里调用requests报错

这是代码

import urllib.request
wa = urllib.request.urlopen('https://www.bilibili.com/')
qw = wa.read()
print(qw)

这是结果

D:\python.exe C:/Users/孤/untitled/dsd.py
Traceback (most recent call last):
  File "C:\Users\孤\untitled\dsd.py", line 9, in <module>
    wa = urllib.request.urlopen('https://www.bilibili.com/')
  File "D:\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "D:\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "D:\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "D:\lib\urllib\request.py", line 555, in error
    result = self._call_chain(*args)
  File "D:\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "D:\lib\urllib\request.py", line 747, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "D:\lib\urllib\request.py", line 523, in open
    response = meth(req, response)
  File "D:\lib\urllib\request.py", line 632, in http_response
    response = self.parent.error(
  File "D:\lib\urllib\request.py", line 561, in error
    return self._call_chain(*args)
  File "D:\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "D:\lib\urllib\request.py", line 641, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Process finished with exit code 1
 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • technologist_09 CSDN技术专家团-009 4天前

    需要添加请求头headers,才能获取页面内容。用下面代码可以实现:

    import urllib.request
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51'}
    url = "https://www.bilibili.com/"
    req = urllib.request.Request(url, headers=headers)
    content = urllib.request.urlopen(req)
    print(content.read().decode())
    
    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 昨天

    非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

    速戳参与调研>>>https://t.csdnimg.cn/Kf0y

    点赞 评论 复制链接分享
  • QA_Assistant 有问必答小助手 2天前

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论 复制链接分享
  • frank2102 小兔子编程 4天前

    限制访问,没有权限访问哔站  https 协议 提供了身份验证与加密通讯方法。

    点赞 评论 复制链接分享

相关推荐