前情回顧
- 🎉上周一發布了一篇博客,只要點贊、評論就能參與抽獎送書!
- 🎉這次讓我們來看看這位博主是怎么抽獎的吧
- 🎉點贊 👍 收藏 ?留言 📝 即可參與抽獎送中秋禮盒🙉
- 🎉文末可領取原始碼~?
詳情可以點擊鏈接 《機器學習入門:基于數學原理的Python實戰》
所以今天帶大家來康康,博主是怎么抽獎噠~
康康有木有傳說中的黑幕
目錄
- 前情回顧
- 1. 如何獲取評論名單?
- 2. 如何抽獎?
- 3. 開獎啦~
- 4. 抽獎啦~
- 最后
1. 如何獲取評論名單?
按下F12查看串列,很快就能找到這些評論的鏈接了~

所以我們需要先獲取這個串列的評論資訊
- 首先使用
&http.Client{}創建一個客戶端的請求 - 然后再通過
NewRequest進行發送POST請求
client := &http.Client{}
reqSpider, err := http.NewRequest("POST", "https://blog.csdn.net/phoenix/web/v1/comment/list/120067923?page="+p+"&size=10", nil)
if err != nil {
log.Fatal(err)
}
reqSpider.Header.Set("content-length", "0") // 書寫請求頭
reqSpider.Header.Set("accept", "*/*")
reqSpider.Header.Set("x-requested-with", "XMLHttpRequest")
respSpider, err := client.Do(reqSpider) // 發送請求
if err != nil {
log.Fatal(err)
}
defer respSpider.Body.Close()
bodyText, err := ioutil.ReadAll(respSpider.Body)
if err != nil {
log.Fatal(err)
}
- 由于
go語言轉json格式沒有那么方便,所以我們必須自己使用進行反序列 - 所以要自己寫一個結構體,推薦一個StringToStruct的網址,百度就行了!
type Result struct {
Code int64 `json:"code"`
Data struct {
Count int64 `json:"count"`
FloorCount int64 `json:"floorCount"`
List []struct {
Info struct {
ArticleID int64 `json:"articleId"`
Avatar string `json:"avatar"`
CommentFromTypeResult struct {
Index int64 `json:"index"`
Key string `json:"key"`
Title string `json:"title"`
} `json:"commentFromTypeResult"`
CommentID int64 `json:"commentId"`
CompanyBlog interface{} `json:"companyBlog"`
CompanyBlogIcon interface{} `json:"companyBlogIcon"`
Content string `json:"content"`
DateFormat string `json:"dateFormat"`
Digg int64 `json:"digg"`
DiggArr []interface{} `json:"diggArr"`
Flag interface{} `json:"flag"`
FlagIcon interface{} `json:"flagIcon"`
LevelIcon interface{} `json:"levelIcon"`
NickName string `json:"nickName"`
ParentID int64 `json:"parentId"`
ParentNickName interface{} `json:"parentNickName"`
ParentUserName interface{} `json:"parentUserName"`
PostTime string `json:"postTime"`
UserName string `json:"userName"`
Vip interface{} `json:"vip"`
VipIcon interface{} `json:"vipIcon"`
Years interface{} `json:"years"`
} `json:"info"`
PointCommentID interface{} `json:"pointCommentId"`
Sub []interface{} `json:"sub"`
} `json:"list"`
PageCount int64 `json:"pageCount"`
} `json:"data"`
Message string `json:"message"`
}
把這個復制到網址上就能生成結構體了


- 然后對回傳的資料進行序列化就行了,
- 然后把所有評論的人放在一個串列當中,
var result Result
_ = json.Unmarshal(bodyText, &result) //byte to json
num := len(result.Data.List)
commentList := result.Data.List
for i:=0 ;i<num; i++ {
var luckPerson LuckyPerson
luckPerson.UserName = commentList[i].Info.UserName
luckPerson.NickName = commentList[i].Info.NickName
tmp = append(tmp, luckPerson) // 存放每一個評論的人
}
- 但是我們要進行一個去重的操作!
func removeRepByMap(slc []LuckyPerson) []LuckyPerson { //去除重復的元素
var result []LuckyPerson //存放回傳的不重復切片
tempMap := map[LuckyPerson]byte{} // 存放不重復主鍵
for _, e := range slc {
l := len(tempMap)
tempMap[e] = 0 //當e存在于tempMap中時,再次添加是添加不進去的,因為key不允許重復
if len(tempMap) != l { // 加入map后,map長度變化,則元素不重復
result = append(result, e) //當元素不重復時,將元素添加到切片result中
}
}
return result
}
到目前為止,我們就拿到全部的去重評論名單啦!
準備抽獎!
2. 如何抽獎?
采用隨機種子進行隨機選取
func lottery(totalPerson []LuckyPerson) LuckyPerson { // 抽取中獎選手
rand.Seed(time.Now().UnixNano()) // 使用隨機種子
index:=rand.Intn(len(totalPerson)) // 生成0到這個串列的長度的一個數字
return totalPerson[index] // 回傳中獎選手
}
3. 開獎啦~
讓我們看看是誰獲獎了啦~
由于一縮再縮,只能縮小到這種程度才能看見抽獎全程,

直接截圖吧~


4. 抽獎啦~
點贊、評論即可參與抽獎獲得一個可可愛愛的神秘中秋禮物哈~ ,
本周日晚上 (9月19號19:30進行抽獎哈)還不能告訴你哈~ 秘密!

最后
小生凡一,期待你的關注,
想要源代碼可以到我掃描下方,然后回復CSDN抽獎~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300247.html
標籤:python
上一篇:【建議收藏】畢設/私活/兼職大佬必備,一個掙錢的開源【SpringBoot+Spring Security+MyBatis Plus】腳手架
