【问题标题】:AttributeError: module 'urllib.response' has no attribute 'status_code'AttributeError:模块“urllib.response”没有属性“status_code”
【发布时间】:2021-07-29 00:09:12
【问题描述】:

我是 python 新手,正在开发一个检查 Ubisoft.com 上用户名可用性的 python 项目。我试图通过输入可能的用户名的单词列表并让代码检查站点是否提供 200 或 404 状态代码来做到这一点,其中 404 是不可用的用户名,200 是可用的用户名。当我运行代码时,我收到此回溯错误消息:

 File "C:\Users\Gibbo\OneDrive\Bureaublad\uPlay\usernamess.py", line 7, in <module>
   response = urllib.response.status_code("https://ubisoftconnect.com/en-US/profile/")
AttributeError: module 'urllib.response' has no attribute 'status_code'

这是我编写的代码

import datetime
import urllib.request
import urllib
import requests

url = "https://ubisoftconnect.com/en-US/profile/"
response = urllib.response.status_code("https://ubisoftconnect.com/en-US/profile/")

print(r.status_code)

url = 'https://ubisoftconnect.com/en-US/profile/'
available = "available.txt"
users = "usernames.txt"
now = datetime.datetime.now ()
if response.status_code == 200:
    print('Available')
elif response.status_code == 404:
    print('Unavailable')


def initialize():
    ascii_banner = pyfiglet.figlet_format("Made  By  Gxzs!!")
    print(ascii_banner)
    print(f"{count()} usernames detected")
    print("Press ENTER to begin checking")
    input("")
    check()

我对 Python 编码还很陌生,所以我无法自己尝试一些东西

【问题讨论】:

    标签: python python-3.x http-status-code-404 http-status-codes


    【解决方案1】:

    urllib 和 requests 是两个独立的包,但您似乎将它们混合在一起。以下是如何分别从他们俩那里获取状态码:

    使用 urllib

    import urllib.request
    
    response = urllib.request.urlopen("https://ubisoftconnect.com/en-US/profile/")
    status_code = response.getcode()
    

    使用请求

    import requests
    
    response = requests.get("https://ubisoftconnect.com/en-US/profile/")
    status_code = response.status_code
    

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2019-02-18
      相关资源
      最近更新 更多