ภาษา
Python เป็นภาษาหนึ่งที่สามารถทำได้เกือบทุกอย่าง สำหรับคนที่กำลังจะหาวิธีในการใช้หรือนำลิ้งวีดีโอมาดาวน์โหลด สามารถทำได้ด้วย library ของภาษา python ที่มีชื่อว่า
requests เป็นตัวที่ใช้สำหรับการร้องขอข้อมูลต่างๆ จาก
Server จากนั้นทางฝ่าย server ก็ทำการส่งข้อมูลที่ทางฝ่าย
client ต้องการตามที่ขอมาในขั้นตอนการดาวน์โหลดวีดีโอนั้นก็ใช้หลักการเดียวกัน ในส่วนของโค้ดคำสั่งการทำงานมีดังนี้
ติดตั้ง requests
ทำการ import requests
ประกาศตัวแปรมารับค่า request ของ ลิ้ง url
โค้ด: เลือกทั้งหมด
url='http://127.0.0.1:5000/video/content.mp4'
req = requests.get(url)
จากนั้นนำลิ้งมาตัด string
ทำการเก็บค่าของ header นั้นเพื่อนำมาเช็ค type
ใช้ for วนค่า dictionary ของ header
จากนั้นใช้ if มาตรวจสอบว่ามี Content-Type
จากนั้นจะได้ค่าของ Content-Type แล้วมาตรวจสอบว่าเป็นไฟล์วีดีโอ
จากนั้นทำการเขียนไฟล์วีดีโอ
โค้ด: เลือกทั้งหมด
path_donw_vdo =vdo_name
with open(path_donw_vdo, 'wb') as f:
f.write(req.content)
print 'Download '+vdo_name+' success'
ผลที่ได้
จากนั้นสังเกตได้ว่ามีไฟล์วีดีโอเพิ่มเข้ามา
ไฟล์ python รวม
โค้ด: เลือกทั้งหมด
import requests
url='http://127.0.0.1:5000/video/content.mp4'
req = requests.get(url)
vdo_name = url.split('/')[-1]
head_req=req.headers
for k, v in head_req.items():
if k == 'Content-Type':
# print k, v
v = v.split('/')
if v[0] == 'video':
path_donw_vdo =vdo_name
with open(path_donw_vdo, 'wb') as f:
f.write(req.content)
print 'Download '+vdo_name+' success'
อ้างอิง
https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
https://www.codementor.io/aviaryan/downloading-files-from-urls-in-python-77q3bs0un
https://www.geeksforgeeks.org/downloading-files-web-using-python/
ภาษา [url=https://www.mindphp.com/คู่มือ/73-คืออะไร/2417-python-คืออะไร.html]Python[/url] เป็นภาษาหนึ่งที่สามารถทำได้เกือบทุกอย่าง สำหรับคนที่กำลังจะหาวิธีในการใช้หรือนำลิ้งวีดีโอมาดาวน์โหลด สามารถทำได้ด้วย library ของภาษา python ที่มีชื่อว่า [url=https://www.mindphp.com/forums/viewtopic.php?f=29&t=58067]requests[/url] เป็นตัวที่ใช้สำหรับการร้องขอข้อมูลต่างๆ จาก [url=https://www.mindphp.com/forums/viewtopic.php?f=29&t=58067]Server[/url] จากนั้นทางฝ่าย server ก็ทำการส่งข้อมูลที่ทางฝ่าย [url=https://www.mindphp.com/คู่มือ/73-คืออะไร/2052-client-server-คืออะไร.html]client[/url] ต้องการตามที่ขอมาในขั้นตอนการดาวน์โหลดวีดีโอนั้นก็ใช้หลักการเดียวกัน ในส่วนของโค้ดคำสั่งการทำงานมีดังนี้
ติดตั้ง requests
[code]
pip install requests
[/code]
ทำการ import requests
[code]
import requests
[/code]
ประกาศตัวแปรมารับค่า request ของ ลิ้ง url
[code]
url='http://127.0.0.1:5000/video/content.mp4'
req = requests.get(url)
[/code]
จากนั้นนำลิ้งมาตัด string
[code]
vdo_name = url.split('/')[-1]
[/code]
ทำการเก็บค่าของ header นั้นเพื่อนำมาเช็ค type
[code]
head_req=req.headers
[/code]
ใช้ for วนค่า dictionary ของ header
[code]
for k, v in head_req.items():
[/code]
จากนั้นใช้ if มาตรวจสอบว่ามี Content-Type
[code]
if k == 'Content-Type':
[/code]
จากนั้นจะได้ค่าของ Content-Type แล้วมาตรวจสอบว่าเป็นไฟล์วีดีโอ
[code]v = v.split('/')
if v[0] == 'video':[/code]
จากนั้นทำการเขียนไฟล์วีดีโอ
[code]
path_donw_vdo =vdo_name
with open(path_donw_vdo, 'wb') as f:
f.write(req.content)
print 'Download '+vdo_name+' success'
[/code]
ผลที่ได้
[img]http://snap.mindphp.com/index.php?view=2019Aug23_5d5fc08a92757[/img]
จากนั้นสังเกตได้ว่ามีไฟล์วีดีโอเพิ่มเข้ามา
[img]http://snap.mindphp.com/index.php?view=2019Aug23_5d5fc12473e60[/img]
ไฟล์ python รวม
[code]
import requests
url='http://127.0.0.1:5000/video/content.mp4'
req = requests.get(url)
vdo_name = url.split('/')[-1]
head_req=req.headers
for k, v in head_req.items():
if k == 'Content-Type':
# print k, v
v = v.split('/')
if v[0] == 'video':
path_donw_vdo =vdo_name
with open(path_donw_vdo, 'wb') as f:
f.write(req.content)
print 'Download '+vdo_name+' success'
[/code]
อ้างอิง
https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
https://www.codementor.io/aviaryan/downloading-files-from-urls-in-python-77q3bs0un
https://www.geeksforgeeks.org/downloading-files-web-using-python/