Go implementation crawl href link

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The first crawl was implemented with a regular expression, and then crawled out of the unnecessary links, and now instead of the Goquery implementation:

Judgeurl Project Judgeurl.gopackage Judgeurlimport ("strings") func Isurl (str string) bool {if strings. Hasprefix (str, "#") | | Strings. Hasprefix (str, "//") | | Strings. Hassuffix (str, ". exe") | | Strings. Hassuffix (str, ": void (0);") {return false} else if strings. Hasprefix (str, "{") && strings. Hassuffix (str, "}") {return false} else if strings. Equalfold (str, "javascript:;") {return false} else {return True}return true}func samepathurl (preUrl string, url string, Mark int) (Newurl string) {last: = Strings. LastIndex (PREURL, "/") if last = = 6 {newurl = preUrl + URL} else {if Mark = = 1 {newurl = preurl[:last] + URL} else {Newpreu RL: = Preurl[:last]newlast: = Strings. LastIndex (Newpreurl, "/") Newurl = Newpreurl[:newlast] + url}}return Newurl}


Weburls_spider Project Main.gopackage mainimport ("FMT" "OS" "Strings" "Github.com/puerkitobio/goquery" test/ Judgeurl ") var urlmap map[string]bool//Prevent duplicate links from entering the dead loop, but getting too many links may be memory overrun func fetch (URL string, Count int) {if count > 1 {//Set The fixed crawl depth is 1 pages return}body, err: = Goquery. NewDocument (URL) if err! = Nil {return}body. Find ("a"). Each (func (i int, aa *goquery. Selection) {href, isexist: = AA. Attr ("href") if isexist = = true {href = strings. Trimspace (HREF) if len (href) > 2 && judgeurl.isurl (href) {if _, OK: = Urlmap[href]; OK = = False {fmt. Println ("Previous URL:", href) if strings. Hasprefix (HREF, "/") | | Strings. Hasprefix (HREF, "./") {href = Judgeurl.samepathurl (url, href, 1)} else if strings. Hasprefix (href, "... /") {href = Judgeurl.samepathurl (url, href, 2)}fmt. Println ("Modified URL:", href) urlmap[href] = truefetch (href, count+1)}}})}func writevalues (outfile string) error {file, ERR: = OS. Create (outfile) if err! = Nil {fmt. Printf ("Failed to create%s file!") ", outfile) return Err}defer file. Close () for k, _: = Range UrlMap {file. WriteString (k + "\ n")}return Nil}func Main () {UrlMap = make (Map[string]bool, 1000000) Fetch ("http://www.hao123.com/", 0) Writevalues ("Urls.dat")}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.