Skip to content

Instantly share code, notes, and snippets.

@goocey
Created August 18, 2020 08:44
Show Gist options
  • Save goocey/a507cefddf3e2a5e9d5c77be3ee65681 to your computer and use it in GitHub Desktop.
Save goocey/a507cefddf3e2a5e9d5c77be3ee65681 to your computer and use it in GitHub Desktop.
youtubeのhtmlで動画URLを抽出する
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
func main() {
fileInfos, _ := ioutil.ReadFile("./youtube.html")
stringReader := strings.NewReader(string(fileInfos))
doc, err := goquery.NewDocumentFromReader(stringReader)
if err != nil {
panic(err)
}
w, _ := os.OpenFile("result.txt", os.O_WRONLY|os.O_CREATE, 0644)
doc.Find("a").Each(func(_ int, s *goquery.Selection) {
url, _ := s.Attr("href")
if strings.Contains(url, "watch?v=") {
fmt.Println(url)
w.WriteString(url + "\n")
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment