Go Language Crawler--the first step

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

Listen to the analysis of Sina Mobile News page, get 5 new news URLs to deposit into the database

Package Linksql


Import (

"Database/sql"

"Log"

_ "Github.com/go-sql-driver/mysql"


)


Func open_url_table () *sql. db{

Db,err:=sql. Open ("MySQL", "Root:root@/mysql")

CheckError (ERR)

Return db

}

Func insertlink (ID int,src string,url string) {

Db:=open_url_table ()

Stmt,err1:=db. Prepare ("INSERT into url_table (Id,src,url) VALUES (?,?,?)")

If err1!=nil{

Log. Fatalln ("Prepare Error", ERR1)

}

_,err2:=stmt. Exec (Id,src,url)

If err2!=nil{

Log. Fatalln ("Exec error")

}

}


Func checkerror (err error) {Listen//Error

If err!=nil{

Log. Fatal (ERR)

}

}


Package Search_sina


Import (

"Net/http"

"Log"

"Gopkg.in/xmlpath.v2"


"Test/linksql"

)



Func get_url (URL string) {//Find new URL

Resp,err:=http. Get (URL)

CheckError (ERR)

Root,err1:=xmlpath. Parsehtml (resp. Body)

CheckError (ERR1)

Path:=xmlpath. Mustcompile ('//dl[@class = ' F_card ']/: /@href ')

Iter:=path. Iter (Root)

I:=1

For ITER. Next () && i<6{

Fmt. PRINTLN (ITER). Node ())

Linksql. Insertlink (i, "Sina", ITER. Node (). String ())

i++

}


}

Func checkerror (err error) {

If err!=nil{

Log. Fatalln (ERR)

}

}


+----+------+-------------------------------------------------------------------------------+| Listen to | listen to SRC Listen | Listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, listen, hear, hear, listen, hear, listen. Listen, listen, listen, listen, listen, listen, listen, hear, hear, hear, hear, hear, hear, hear, listen, hear, listen, |+----+------+------------------ -------------------------------------------------------------+| Listen to the 1 listen to Sina | Listen to Http://tech.sina.cn/tech_zt/meitu_ Ipo?vt=4&pos=18 Listen, listen, listen, listen, hear, hear, listen, listening, hearing, listen, hear, listen, hear, hear, listen, and hear. Listen to listen to Sina listening to listening to the listening and hearing of 2 | | | Listen to listen to listen to Sina listen to listen to listen to listening to hear listening to listen to listen to listen to listen to listen to listening to hear (listening to hearing) | | Listen to listen to Sina listening to listening to the listening and hearing of 4 | | | Listen to listen to the Sina listen to the 5 listen to http://tech.sina.cn/i/gj/2016-12-15/detail-ifxytqav9326149.d.html?vt=4&pos=18 listen to |+----+------+ + ------------------------------------------------------------------------------+


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.