ไม่มี Validate Method มาตรฐาน เพราะเกือบทุกอย่างเป็น Valid URL ได้ มี Punctuation Rules สำหรับแยกส่วน ถ้าไม่มี Punctuation ก็ยังเป็น Valid URL
ขึ้นอยู่กับสถานการณ์ ใช้วิธีต่างๆ:
- Trust Data แค่เช็ค Protocol HTTP: ใช้
urlparse - ต้องการ True URL จริง: ใช้ Regex ที่ซับซ้อน
- ต้องการเช็คว่าเป็น Real Web Address: ใช้ urllib.urlopen()
ตัวอย่าง: เช็ค Real URL
import urllib
try:
urllib.urlopen(url)
except IOError:
print("Not a real URL")
หมายเหตุ: ใน Python 3 ใช้ urllib.request.urlopen()