|
本帖最后由 waini1110 于 2018-6-9 22:56 编辑
编译好了之后,把user.json扔进二进制同级目录。
user.json 内容:
[ol]{ "username": "username", "password": "password"}[/ol]复制代码
下面是go代码,做了判断,并发10次访问,不知道会不会被block掉。
[ol]package mainimport ( "encoding/json" "fmt" "io/ioutil" "math/rand" "net/http" "net/http/cookiejar" "os" "regexp" "strconv" "strings" "time" "github.com/PuerkitoBio/goquery")var ( LoginUrl = "https://www.hs2v.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1")type User struct { Username string `json:username` Password string `json:password`}type Client struct { Client *http.Client Integral int Ch chan string User}func (c *Client) Login() { jar, err := cookiejar.New(nil) if err != nil { fmt.Printf("cookie error:%s", err) } c.Client.Jar = jar req, err := http.NewRequest("POST", LoginUrl, strings.NewReader("username="+c.User.Username+"&password="+c.User.Password)) if err != nil { fmt.Printf("httpRequest error:\r\n", err) os.Exit(1) } req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36") req.Header.Set("Host", "www.hs2v.com") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, _ := c.Client.Do(req) defer resp.Body.Close() doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { fmt.Printf("Parsing HTML error%s:\r\n", err) os.Exit(1) } html, _ := doc.Html() if ok := strings.Contains(html, "登录失败"); ok { fmt.Printf("Login failure\r\n") os.Exit(1) } c.Visit()}func (c *Client) Visit() { r := rand.New(rand.NewSource(time.Now().UnixNano())) for x := 0; x c.Integral { c.Integral = a } fmt.Println(c.Integral) } resp, err := c.Client.Get("https://www.hs2v.com/forum.php") defer resp.Body.Close() if err != nil { fmt.Printf("visit error:%s\r\n", err) os.Exit(1) } doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { fmt.Printf("Parsing HTML error:%s\r\n", err) os.Exit(1) } fmt.Printf("your Integral is: %s\r\n", doc.Find("#extcreditmenu").Text())}func main() { var user User data, err := ioutil.ReadFile("user.json") if err != nil { fmt.Printf("read file faild:%s", err) } json.Unmarshal(data, &user) client := Client{ Client: &http.Client{}, Integral: 0, Ch: make(chan string, 1), User: user, } client.Login() fmt.Println("Closed after 5 seconds") time.Sleep(5 * time.Second)}[/ol]复制代码
|
|