用 scala 写了一个 v 站的签到脚本,但是最后总是 302,大家帮我分析分析?

2016-07-04 21:55:49 +08:00
 caixiexin

最近在学习 scala ,用 v 站签到来练练手。 这个签到脚本主要是参考 https://github.com/lqccan/v2ex-sign/blob/master/v2ex.py 这位 v 友写的签到脚本,自己用 scala 实现了一下,登录是没问题的,但是到最后一步 /mission/daily/redeem?once=xxx 时却失败了,返回 302 ,跳转回 /mission/daily 页面。 以下是代码:

import java.net.HttpCookie



import scalaj.http.{Http, HttpResponse}


object V2Sigin2 {

  def main(args: Array[String]): Unit = {
    val signPage: HttpResponse[String] = Http("http://www.v2ex.com/signin").asString
    val html = signPage.body

    val cookieMap: scala.collection.mutable.Map[String, HttpCookie] = scala.collection.mutable.Map[String, HttpCookie]()
    signPage.cookies.foreach(f => cookieMap.put(f.getName, f))


    val nameMatch = "type=\"text\" class=\"sl\" name=\"([a-f0-9]{64,64})\"".r findFirstMatchIn html
    val pwdMatch = "type=\"password\" class=\"sl\" name=\"([a-f0-9]{64,64})\"".r findFirstMatchIn html
    val onceMatch = "value=\"(\\d+)\" name=\"once\"".r findFirstMatchIn html
    val nameName = if (nameMatch.isEmpty) "" else nameMatch.get.group(1)
    val pwdName = if (pwdMatch.isEmpty) "" else pwdMatch.get.group(1)
    val once = if (onceMatch.isEmpty) "" else onceMatch.get.group(1)
    val next = "/"
    val loginResult = Http("http://www.v2ex.com/signin").postForm(
      Seq(nameName -> "username",
        pwdName -> "password",
        "once" -> once,
        "next" -> next
      )
    ).headers(Seq("Referer" -> "http://www.v2ex.com/signin",
      "User-Agent" -> "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"
    ))
      //      .cookies(signPage.cookies)
      .cookies(cookieMap.values.toSeq)
      .asString
    loginResult.cookies.foreach(f => cookieMap.put(f.getName, f))


    if (loginResult.code == 302) {
      val dailyResult = Http("http://www.v2ex.com/mission/daily")
        //      .cookies(loginResult.cookies)
        .cookies(cookieMap.values.toSeq)
        .asString
      dailyResult.cookies.foreach(f => cookieMap.put(f.getName, f))
      if (dailyResult.code != 200) {
        println("没有登录无法签到!")
        return
      }
      if (dailyResult.body.indexOf("fa-ok-sign") > -1) {
        println("本日已签到!")
      } else {

        val dailyMatch = "(/mission/daily/redeem\\?once=\\d+)".r findFirstMatchIn dailyResult.body
        val daily = if (dailyMatch.isEmpty) "" else dailyMatch.get.group(1)
        val signResult = Http("http://www.v2ex.com" + daily)
          .cookies(cookieMap.values.toSeq)
          //          .cookies(loginResult.cookies)
          //          .cookies(dailyResult.cookies)
          .headers(
          Seq("Referer" -> "http://www.v2ex.com/mission/daily",
            "User-Agent" -> "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0",
            "Host" -> "www.v2ex.com",
            "Connection" -> "keep-alive"
          )
        ).asString
        println(signResult.code)
        signResult.cookies.foreach(f => cookieMap.put(f.getName, f))
        println("签到成功!")

      }
    } else {
      println("登录失败!")

    }
    println("============")
    println(cookieMap)


  }

}

一开始以为是 cookie 问题,因为那位 v 友用的 python 的 requests.Session()是能自动管理 cookie 的,而我用的 http 包没有管理上下文的功能,但是不管我怎么设置 cookie ,最终都是返回 302 ,问题出在哪里呢? 代码写的不是很好,大家轻拍。。

2402 次点击
所在节点    问与答
3 条回复
hicdn
2016-07-04 22:33:56 +08:00
#!/bin/sh

cookie=''

UA=''

url=$( curl 'https://v2ex.com/mission/daily' -A "$UA" -H 'pragma: no-cache' -H 'dnt: 1' -H 'accept-language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4' -H 'upgrade-insecure-requests: 1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'cache-control: no-cache' -H "cookie: $cookie" -H 'referer: https://v2ex.com/' | fgrep 'location.href ' | awk -F"'" '{print $(NF-1)}')

curl "https://v2ex.com$url" --referer "https://v2ex.com/mission/daily" -H "cookie: $cookie" -A "$UA" -H 'pragma: no-cache' -H 'dnt: 1' -H 'accept-encoding: gzip, deflate, sdch' -H 'accept-language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4' -H 'upgrade-insecure-requests: 1' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'cache-control: no-cache'
mx1700
2016-07-05 08:05:19 +08:00
领取每日奖励的请求本来就是个 302 跳转吧
caixiexin
2016-07-05 08:44:58 +08:00
@mx1700 恩,不过 302 之后并没有领取成功

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/290257

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX