Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python AttributeError: 'module' object has no attribute 'urlopen' - แก้ไข Python 3

โมดูล urllib เปลี่ยนวิธีเข้าถึงฟังก์ชัน request ใน Python 3 หมายถึงถ้าพยายามอ้างอิง "urlopen" function แบบ Python 2 จะเจอ error AttributeError: 'module' object has no attribute 'urlopen'

คู่มือนี้พูดถึง error นี้หมายถึงอะไร ทำไมจึงเกิดขึ้น พร้อมตัวอย่าง ช่วยเรียนรู้วิธีแก้

AttributeError: 'module' object has no attribute 'urlopen'

โมดูล "urllib" ให้ฟังก์ชันเกี่ยวกับการเปิด URLs และอ่านข้อมูลจากเว็บไซต์ Syntax การใช้ library ต่างกันระหว่าง Python 2 และ 3

ใน Python 2 "urlopen" เป็นส่วนของ "urllib" module สามารถ import เข้าโค้ดได้ด้วย urllib.urlopen ใน Python 3 "urlopen" เป็นส่วนของ "request" module ภายใน "urllib" method:

  • Python 2: urllib → urlopen
  • Python 3: urllib → request → urlopen

"request" module คือที่รวม web request functions หลายตัวใน "urllib" package AttributeErrors เกิดเมื่อพยายามเข้าถึง attribute จาก module ที่ไม่มี attribute นั้น ในกรณีนี้ "urlopen" ไม่ใช่ส่วนของ "urllib" module

ตัวอย่างสถานการณ์ (An Example Scenario)

สร้างโปรแกรมดึงข้อมูลจาก service ชื่อ JSONPlaceholder service นี้ให้ dummy data ใช้ในตัวอย่าง

ดึง single post ID #2 เริ่ม import urllib module:

import urllib

Statement นี้ให้ใช้ functions ภายใน "urllib" module ในโปรแกรม

ต่อไป ทำ request ด้วย "urlopen" จะดึงข้อมูลจาก endpoint บน JSONPlaceholder API:

data = urllib.urlopen("https://jsonplaceholder.typicode.com/posts/2")
contents = data.read()
print(as_json)

โค้ดอ่านข้อมูล post ID #2 บน JSONPlaceholder API พิมพ์ response จาก request ที่ format เป็น JSON string ไปคอนโซล ด้วย print statement รันโค้ด:

Traceback (most recent call last):
File "main.py", line 4, in 
data = urllib.urlopen("https://jsonplaceholder.typicode.com/posts/2")
AttributeError: module 'urllib' has no attribute 'urlopen'

โปรแกรม error

วิธีแก้ (The Solution)

คุณใช้ Python 2 syntax เข้าถึง "urlopen" method "urlopen" ไม่ใช่ attribute ของ "urllib" ใน Python 3 คุณอ้างอิง "urlopen" เป็น attribute ของ "urllib" ทำให้โปรแกรมรันไม่ได้

แก้: import "request" module จาก "urllib" module module นี้มี "urlopen" method:

import urllib.request

เปลี่ยนโค้ด ให้อ้างอิง urllib.request module ตอนดึงข้อมูลจาก JSONPlaceholder API:

data = urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts/2")
contents = data.read().decode('utf-8')
print(contents)

โค้ดอ่าน contents ของ API endpoint ที่เราเรียก รันโค้ด:

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

คุณ decode response ที่ได้ ด้วย decode() method ทำเพื่ออ่านค่าที่คืนจาก urlopen() method เป็น string

สรุป

ฟังก์ชัน "urlopen" ไม่ใช่ object ของ "urllib" ใน Python 3 เข้าถึงฟังก์ชันนี้: import "urllib.request" และอ้างอิง urllib.request.urlopen() function

ตอนนี้คุณมีความรู้แก้ Python error นี้อย่างมืออาชีพแล้ว!