Created
August 18, 2020 08:44
-
-
Save g00cey/a507cefddf3e2a5e9d5c77be3ee65681 to your computer and use it in GitHub Desktop.
youtubeのhtmlで動画URLを抽出する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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