Golang : How to extract video or image files from html source code
There are times when I need to download a certain video that I watched using browser and I need to find the video's source file to download. The following is a simple example that demonstrate how to extract video or image files from HTML source code. Basically what it does is to grab the entire HTML data and filter out the file names with regular expression.
Here you go!
package main
import (
"io/ioutil"
"log"
"net/http"
"regexp"
)
func SearchForVideoLinks(url string) {
log.Println("Parsing : ", url)
// Request the HTML page.
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Unable to get URL with status code error: %d %s", resp.StatusCode, resp.Status)
}
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
videoRegExp := regexp.MustCompile(`<video[^>]+`)
sourceRegExp := regexp.MustCompile(`<source[^>]+`)
videoMatchSlice := videoRegExp.FindAllStringSubmatch(string(htmlData), -1)
sourceMatchSlice := sourceRegExp.FindAllStringSubmatch(string(htmlData), -1)
for _, item := range videoMatchSlice {
log.Println("Video found : ", item)
for _, sourceItem := range sourceMatchSlice {
log.Println("Source found : ", sourceItem)
}
}
}
func SearchForImageLinks(url string) {
log.Println("Parsing : ", url)
// Request the HTML page.
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("Unable to get URL with status code error: %d %s", resp.StatusCode, resp.Status)
}
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
imageRegExp := regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)
subMatchSlice := imageRegExp.FindAllStringSubmatch(string(htmlData), -1)
for _, item := range subMatchSlice {
log.Println("Image found : ", item[1])
}
}
func main() {
SearchForImageLinks("https://socketloop.com")
SearchForImageLinks("https://golang.org")
SearchForVideoLinks("https://cdpn.io/caraya/fullpage/FckCd")
}
Sample output :
2020/02/27 09:49:08 Parsing : https://socketloop.com
2020/02/27 09:49:09 Image found : https://d1ohg4ss876yi2.cloudfront.net/socketloop-logo1.png
2020/02/27 09:49:09 Image found : //pixel.quantserve.com/pixel/p-31iz6hfFutd16.gif?labels=Domain.socketloop_com,DomainId.22847
2020/02/27 09:49:09 Image found : https://sb.scorecardresearch.com/p?c1=2&c2=20015427&cv=2.0&cj=1
2020/02/27 09:49:09 Parsing : https://golang.org
2020/02/27 09:49:09 Image found : /lib/godoc/images/go-logo-blue.svg
2020/02/27 09:49:09 Image found : /lib/godoc/images/cloud-download.svg
2020/02/27 09:49:09 Image found : /lib/godoc/images/footer-gopher.jpg
2020/02/27 09:49:09 Parsing : https://cdpn.io/caraya/fullpage/FckCd
2020/02/27 09:49:10 Video found : [<video id='video' controls="controls" preload='none'
width="600" poster="http://media.w3.org/2010/05/sintel/poster.png"]
2020/02/27 09:49:10 Source found : [<source id='mp4' src="http://media.w3.org/2010/05/sintel/trailer.mp4" type='video/mp4'/]
2020/02/27 09:49:10 Source found : [<source id='webm' src="http://media.w3.org/2010/05/sintel/trailer.webm" type='video/webm'/]
2020/02/27 09:49:10 Source found : [<source id='ogv' src="http://media.w3.org/2010/05/sintel/trailer.ogv" type='video/ogg'/]
See also : Golang : How to extract links from web page ?
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14k Golang : How to pass map to html template and access the map's elements
+5.5k Unix/Linux/MacOSx : Get local IP address
+12.1k Golang : Encrypt and decrypt data with x509 crypto
+23.7k Golang : Call function from another package
+30.6k error: trying to remove "yum", which is protected
+5k Golang : Customize scanner.Scanner to treat dash as part of identifier
+19.9k Golang : Check if os.Stdin input data is piped or from terminal
+7.1k Golang : Example of custom handler for Gorilla's Path usage.
+16.2k Golang : Convert slice to array
+86.4k Golang : How to convert character to ASCII and back
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+11.7k Golang : Convert decimal number(integer) to IPv4 address