golang 领取v2ex每日奖励。命令行参数1 =用户名,命令行参数2 =密码。%v2ex_dialy username pwd

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 领取v2ex每日奖励。命令行参数1 =用户名,命令行参数2 =密码。%v2ex_dialy username pwd相关的知识,希望对你有一定的参考价值。

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"regexp"
	"strings"
)

var loginUrl = "http://v2ex.com/signin"
var missionUrl = "http://v2ex.com/mission/daily"

var username = os.Args[1]
var password = os.Args[2]

type myCookieJar struct {
	cookies []*http.Cookie
}

func (c *myCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
	if c.cookies == nil {
		c.cookies = make([]*http.Cookie, 0)
	}

	for _, it := range cookies {
		c.cookies = append(c.cookies, it)
	}
}

func (c *myCookieJar) Cookies(u *url.URL) []*http.Cookie {
	return c.cookies
}

func main() {
	cookieJar := &myCookieJar{}
	client := http.Client{Jar: cookieJar}
	// login
	loginreq, _ := http.NewRequest("GET", loginUrl, nil)
	loginresp, _ := client.Do(loginreq)
	loginHtml, _ := ioutil.ReadAll(loginresp.Body)
	re := regexp.MustCompile(`<input type="hidden" value="(\d+)" name="once" />`)
	matched := re.FindStringSubmatch(string(loginHtml))
	params := url.Values{"next": {"/", "/"}, "u": {username}, "p": {password}, "once": {matched[1]}}
	req, _ := http.NewRequest("POST", loginUrl, strings.NewReader(params.Encode()))
	req.Header.Add("Host", "v2ex.com")
	req.Header.Add("Origin", "http://v2ex.com")
	req.Header.Add("Referer", loginUrl)
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	for _, c := range cookieJar.cookies {
		req.AddCookie(c)
	}
	client.Do(req)

	// try finish mission
	missionReq, _ := http.NewRequest("GET", missionUrl, nil)
	missionResp, _ := client.Do(missionReq)
	missionHtml, _ := ioutil.ReadAll(missionResp.Body)
	missionHtmlStr := string(missionHtml)
	if strings.Index(missionHtmlStr, "每日登录奖励已领取") > 0 {
		fmt.Println("每日登录奖励已领取")
	} else {
		re = regexp.MustCompile(`location.href = '(.*)'`)
		matched = re.FindStringSubmatch(missionHtmlStr)
		req, _ = http.NewRequest("GET", "http://www.v2ex.com"+matched[1], nil)
		client.Do(req)
	}
}

Java 实现掘金每日自动签到领取矿石奖励

pom文件引入依赖

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-server</artifactId>
			<version>3.0.1</version>
		</dependency>

核心代码示例

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;


public class JueJinSignIn 

    private static String webDriver = "webdriver.chrome.driver";
    private static String webDriverPath ="E:\\\\work_space\\\\reptile\\\\src\\\\main\\\\resources\\\\chromedriver\\\\chromedriver.exe";
    private static WebDriver driver = null;

    public static void main(String[] args) throws InterruptedException 
        System.setProperty(webDriver, webDriverPath);
        // ChromeOptions
        ChromeOptions chromeOptions = new ChromeOptions();
        // 设置后台静默模式启动浏览器
        //   chromeOptions.addArguments("--headless");
        //添加用户cookies数据
        chromeOptions.addArguments("--user-data-dir=C:\\\\Users\\\\liuya\\\\AppData\\\\Local\\\\Google\\\\Chrome\\\\User Data1");
        //启动浏览器
        driver = new ChromeDriver(chromeOptions);
        while (true)
            start();
        
    

    public static void start() throws InterruptedException 
        driver.get("https://juejin.cn/");
        Thread.sleep(500);
        WebElement signedin = driver.findElement(By.xpath("//button[@class='btn signedin-btn']"));
        signedin.sendKeys(Keys.ENTER);
        Thread.sleep(500);
        WebElement signed = driver.findElement(By.xpath("//button[@class='signedin btn']"));
        signed.sendKeys(Keys.ENTER);
        Thread.sleep(500);
        WebElement btn = driver.findElement(By.xpath("//div[@class='btn-area']/button[@class='btn']"));
        btn.sendKeys(Keys.ENTER);
        Thread.sleep(500);
        WebElement LuckyDrawBtn = driver.findElement(By.id("turntable-item-0"));
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("arguments[0].scrollIntoView(true);", LuckyDrawBtn);
        LuckyDrawBtn.click();
        Thread.sleep(500);
        System.out.println(driver.getPageSource());
        List<WebElement> elements = (List<WebElement>) jse.executeScript("return jQuery.find('.lottery_modal byte-modal lottery_modal_lucky v-transfer-dom')");
        WebElement reward= elements.get(0).findElement(By.xpath("//div[@class='wrapper']/button[@class='submit']"));
        reward.sendKeys(Keys.ENTER);
        //等待24小时
        Thread.sleep(24*60*60*1000);
    


 

 

 

以上是关于golang 领取v2ex每日奖励。命令行参数1 =用户名,命令行参数2 =密码。%v2ex_dialy username pwd的主要内容,如果未能解决你的问题,请参考以下文章

Java 实现掘金每日自动签到领取矿石奖励

Java 实现掘金每日自动签到领取矿石奖励

python v2ex自动登录,领取金币脚本

golang获取命令行参数

golang命令行参数解析

golang命令行参数