Reputation: 3
I'm trying to filter terminal output of this, but I'm not sure how to do this. I've tried putting the output into slices, but they output separate slices, and I'm not sure how to join these slices together to make one slice. I haven't found anything helpful when searching for filtering output, so I'm hoping someone can give me a solution. I want to get //abs.twimg.com
func main() {
profileURL := "url"
resp, err := soup.Get(profileURL)
check("Couldn't send GET request:", err)
parse := soup.HTMLParse(resp)
find := parse.Find("head").FindAll("link")
for _, i := range find {
links := []string{i.Attrs()["href"]}
log.Println(links)
}
}
Output:
2020/06/09 08:54:55 [//abs.twimg.com ]
2020/06/09 08:54:55 [//api.twitter.com ]
2020/06/09 08:54:55 [//pbs.twimg.com ]
2020/06/09 08:54:55 [//t.co ]
2020/06/09 08:54:55 [//video.twimg.com ]
2020/06/09 08:54:55 [//abs.twimg.com ]
2020/06/09 08:54:55 [//api.twitter.com ]
2020/06/09 08:54:55 [//pbs.twimg.com ]
2020/06/09 08:54:55 [//t.co ]
2020/06/09 08:54:55 [//video.twimg.com ]
2020/06/09 08:54:55 [https://abs.twimg.com/responsive-web/web/polyfills.604422d4.js ]
2020/06/09 08:54:55 [https://abs.twimg.com/responsive-web/web/vendors~main.55bd4704.js ]
2020/06/09 08:54:55 [https://abs.twimg.com/responsive-web/web/i18n-rweb/en.15808594.js ]
2020/06/09 08:54:55 [https://abs.twimg.com/responsive-web/web/i18n-horizon/en.d212af84.js ]
2020/06/09 08:54:55 [https://abs.twimg.com/responsive-web/web/main.cc767dc4.js ]
2020/06/09 08:54:55 [/manifest.json ]
Upvotes: 0
Views: 324
Reputation: 4204
I hope this helps!
import (
"log"
"strings"
)
func main() {
profileURL := "url"
resp, err := soup.Get(profileURL)
check("Couldn't send GET request:", err)
parse := soup.HTMLParse(resp)
find := parse.Find("head").FindAll("link")
filter := make([]string, 0)
for _, i := range find {
// map[string]string
if strings.Contains(i.Attrs()["href"], "//abs.twimg.com") {
filter = append(filter, i.Attrs()["href"])
}
}
log.Println(filter)
}
Upvotes: 1
Reputation: 1441
This should do it:
find := parse.Find("head").FindAll("link")
links := make([]string, 0, 1)
for _, i := range find {
links = append(links, i.Attrs()["href"])
}
log.Println(links)
If you know the length of the slice, replace 0,1 with the appropriate values during initialization
Upvotes: 0