วิธีการใช้คำสั่ง wget « SpaLinux.com
วิธีการใช้คำสั่ง wget
Friday 22 January 2010 @ 3:04 pm

wget เป็นโปรแกรมที่ใช้เพื่อดาวน์โหลดไฟล์จากเว็บไซต์ได้ ในรูปแบบ text console วิธีการใช้งานก็ง่ายคือรันคำสั่ง wget ตามด้วย URL ที่ต้องการดาวน์โหลด ผลลัพธ์ที่ได้จะถูกบันทึกเป็นไฟล์ของเว็บเพจหน้านั้นๆ

รูปแบบการใช้งาน

$ wget [option]... [URL]...

ตัวอย่างเช่น ต้องการดาวน์โหลดเว็บเพจหน้าแรกของ www.google.co.th

$ wget 'http://www.google.co.th'
--13:52:23--  http://www.google.co.th/
           => `index.html'
Resolving www.google.co.th... 72.14.203.147, 72.14.203.103, 72.14.203.104, ...
Connecting to www.google.co.th|72.14.203.147|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
    [  <=>                                                                                ] 6,726         11.96K/s
13:52:24 (11.96 KB/s) - `index.html' saved [6726]

จากตัวอย่าง ผลลัพธ์ที่ได้จะถูกบันทึกเป็นชื่อ index.html

หมายเหตุ แนะนำให้ใส่เครื่องหมายคำพูด ‘  คลอบ URL เพราะบางตัวอักษรอาจมีความหมายใน SHELL

$ cat index.html
<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=windows-874"><title>Google</title>
...

ระบุออปชั่น ‘–help’ เพื่อดูวิธีการใช้งาน พร้อมทั้งออปชั่นต่างๆ

$ wget --help
GNU Wget 1.10.2 (Red Hat modified), a non-interactive network retriever.
Usage: wget [OPTION]... [URL]...
Mandatory arguments to long options are mandatory for short options too.
Startup:
  -V,  --version           display the version of Wget and exit.
  -h,  --help              print this help.
  -b,  --background        go to background after startup.
  -e,  --execute=COMMAND   execute a `.wgetrc'-style command.

ในบทความนี้ขอแนะนำการใช้ออปชั่นที่ผู้เขียนใช้บ่อยๆ

ดูรายละเอียดการโหลด (debug)

ระบุออปชั่น ‘-d’ เพื่อดูรายละเอียดการดาวน์โหลด เช่นดู HTTP Header, Protocol, Cookie ที่เกิดขึ้นในการดาวน์โหลด

$ wget -d 'http://www.google.co.th'
DEBUG output created by Wget 1.10.2 (Red Hat modified) on linux-gnu.
--14:16:24--  http://www.google.co.th/
           => `index.html'
Resolving www.google.co.th... 72.14.203.105, 72.14.203.106, 72.14.203.99, ...
Caching www.google.co.th => 72.14.203.105 72.14.203.106 72.14.203.99 72.14.203.104 72.14.203.103 72.14.203.147
Connecting to www.google.co.th|72.14.203.105|:80... connected.
Created socket 3.
Releasing 0x000000000083d390 (new refcount 1).
---request begin---
GET / HTTP/1.0
User-Agent: Wget/1.10.2 (Red Hat modified)
Accept: */*
Host: www.google.co.th
Connection: Keep-Alive
---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.0 200 OK
Date: Fri, 22 Jan 2010 07:16:29 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=windows-874
Set-Cookie: PREF=ID=79a6955a36392a2d:TM=1264144589:LM=1264144589:S=c99kK7cOeBwCp4KG; expires=Sun, 22-Jan-2012 07:16:29 GMT; path                                             =/;
domain=.google.co.th
Set-Cookie: NID=31=h8-gos7iGB-MXzlXOo4C1_CRqzSNE6wu5Os2WBu6C-jm7tPhrlj4kJp1aiENCM66f388IScBzce4Q-6x7rwbfxUUbp2DGAIcxXu1zwSGy6zKH                                            
cjUQ2_59hiym_wuaRt2; expires=Sat, 24-Jul-2010 07:16:29 GMT; path=/; domain=.google.co.th; HttpOnly
Server: gws
X-XSS-Protection: 0
---response end---
200 OK
cdm: 1 2 3 4 5 6 7 8
Stored cookie google.co.th -1 (ANY) / <permanent> <insecure> [expiry 2012-01-22 14:16:29] PREF ID=79a6955a36392a2d:TM=1264144589                                            
:LM=1264144589:S=c99kK7cOeBwCp4KG
cdm: 1 2 3 4 5 6 7 8
Stored cookie google.co.th -1 (ANY) / <permanent> <insecure> [expiry 2010-07-24 14:16:29] NID 31=h8-gos7iGB-MXzlXOo4C1_CRqzSNE6w                                            
u5Os2WBu6C-jm7tPhrlj4kJp1aiENCM66f388IScBzce4Q-6x7rwbfxUUbp2DGAIcxXu1zwSGy6zKHcjUQ2_59hiym_wuaRt2
Length: unspecified [text/html]
    [  <=>                                                                                ] 6,702          6.21K/s
Closed fd 3
14:16:31 (6.20 KB/s) - `index.html' saved [6702]

ดาวน์โหลดต่อไฟล์ (continue)

สามารถดาวน์โหลดไฟล์ต่อจากที่ดาวน์โหลดยังไม่ครบได้

เช่นต้องการดาวน์โหลดไฟล์ iso ที่มีขนาดใหญ่มาก แล้วดาวน์โหลดครั้งแรกยังไม่ครบ

$ wget 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--14:09:51--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           => `Fedora-11-x86_64-Live.iso'
Resolving www.mirror.in.th... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 724,097,024 (691M) [application/octet-stream]
 0% [                                                                                     ] 1,090,066     87.82K/s  ETA 2:13:52

หมายเหตุ ในระหว่างการดาวน์โหลด หากต้องการยกเลิกให้กด [Ctrl-C]

ครั้งแรกดาวน์โหลดไฟล์ได้แค่ 1 Meg กว่าๆ

$ ls -l
total 1080
-rw-r--r-- 1 user1  users  1098754 2010-01-22 14:10 Fedora-11-x86_64-Live.iso

ถ้าต้องการดาวน์โหลดต่อ ต้องระบุออปชั่น ‘-c’

$ wget -c 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--14:11:46--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           => `Fedora-11-x86_64-Live.iso'
Resolving www.mirror.in.th... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 206 Partial Content
Length: 724,097,024 (691M), 722,998,270 (690M) remaining [application/octet-stream]
 0% [                                                                                     ] 1,401,045     94.59K/s  ETA 2:04:50
$ ls -l
total 1384
-rw-r--r-- 1 user1  users  1409733 2010-01-22 14:11 Fedora-11-x86_64-Live.iso

จำกัดความเร็วการโหลด

ต้องการโหลดไฟล์แต่ไม่ต้องการเบียดบังความเร็วการใช้อินเตอร์เน็ตจากคนอื่นมากเกินไป เราสามารถจำกัดความเร็วในการโหลดของเราได้ ด้วยออปชั่น ‘–limit-rate’

เช่นต้องการจำกัดไว้ที่ 10 KBytes/s

$ wget --limit-rate 10000 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--14:25:14--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
           => `Fedora-11-x86_64-Live.iso'
Resolving www.mirror.in.th... 61.7.253.242
Connecting to www.mirror.in.th|61.7.253.242|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 724,097,024 (691M) [application/octet-stream]
 0% [                                                                                     ] 62,618         9.77K/s ETA 20:07:01

หมายเหตุ หน่วยของออปชั่น ‘–limit-rate’ เป็น bytes per second

โหลดผ่าน proxy

หากต้องใช้ proxy ในการโหลด ต้องมีการระบุตัวแปร ‘http_proxy’ ไว้ใน shell ก่อนรันคำสั่ง เช่น IP ของ proxy คือ 192.168.1.1 และพอร์ต proxy คือ 3128 ถ้าใช้ BASH สามารถทำได้โดย

$ export http_proxy=http://192.168.1.1:3128

ใช้คำสั่ง wget พร้อมระบุออปชั่น ‘–proxy’ เพื่อให้โหลดผ่าน proxy

$ wget --proxy 'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--2010-01-22 14:30:26--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso'
 0% [                                                                                     ] 63,434      96.4K/s

แต่ถ้า proxy ต้องระบุ user, password ด้วย จะขึ้น error ดังนี้

$ wget --proxy  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--2010-01-22 14:31:06--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 407 Proxy Authentication Required
2010-01-22 14:31:06 ERROR 407: Proxy Authentication Required.
ใช้ออปชั่น '--proxy-user' และ '--proxy-password' เพื่อระบุ user, password ในการใช้ proxy
$ wget --proxy-user=prxuser --proxy-password=prx1234  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--2010-01-22 14:30:26--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso'
 0% [                                                                                     ] 63,434      96.4K/s

ไฟล์คอนฟิก .wgetrc

แทนที่จะต้องระบุออปชั่น proxy ทุกครั้ง สามารถสร้างเป็นไฟล์คอนฟิกสำหรับใช้คำสั่ง wget โดยสร้างไฟล์ชื่อ .wgetrc (มีเครื่องหมายจุดนำหน้าชื่อไฟล์ด้วย) ไว้ใน HOME ของผู้ใช้งาน

$ cat ~/.wgetrc
http_proxy=http://192.168.1.1:3128/
proxy_user=prxuser
proxy_password=prx1234

หลังจากสร้างไฟล์คอนฟิก .wgetrc แล้ว เวลาใช้คำสั่ง wget ก็ไม่ต้องระบุออปชั่นใดๆ อีก

$ wget  'http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso'
--2010-01-22 14:37:46--  http://www.mirror.in.th/osarchive/fedora/releases/11/Live/x86_64/Fedora-11-x86_64-Live.iso
Connecting to 192.168.1.1:3128... connected.
Proxy request sent, awaiting response... 200 OK
Length: 724097024 (691M) [application/octet-stream]
Saving to: `Fedora-11-x86_64-Live.iso.1'
 0% [                                                                                     ] 80,810      95.8K/s

ข้อมูลอ้างอิง

Tags:




 3 responses to “วิธีการใช้คำสั่ง wget”

  •   daniel14fw wrote:

    ขอบคุณครับ กะลังเพิ่งหัดใช้พอดี มีประโยชน์มากครับ

  •   adisak wrote:

    ขอบคุณมากครับ

    ถ้าเกิด ผม อยาก จะ ดึง ข้อมูลใน web แต่ จะไม่เอา พวกรูปภาพเลย
    จะเอาเพียงแต่ content อย่างเดี่ยวอ่าครับ

    ผมต้อง ใช้ คำสั่งไหน อ่าครับ

  •   editor wrote:

    ใช้ wget ก็ได้ครับ แต่ต้องระบุออปชั่น ลองใช้ –accept ดูครับ

Leave a comment