[Go language combat] (6) Go language crawling Blink comment | Little Blink has a big move

write in front

I sent one before blink, I just wanted to blogsend python in the comments in it, but there blinkwere too many people, so I blinksent another javabook . Then let's take a look at how the Go language crawls the comments of blink~

insert image description here
A while ago, I wrote a lottery Go语言on the blog 评论区, and today I will bring you the blink评论区current lottery situation!

1. Find blink

As for CSDN, you have to switch 新版to see blink! I had the old version, so I switched to the new version first.
we found it
insert image description here

2. Analysis page

We found that there is no corresponding interface to display comments, so after we click more insert image description here
, this will appear, and this is our crawling address!
insert image description here
Then we look at this url, the return is the comment information we need!
insert image description here
Then we look at the response parameters!
insert image description here

  • pageNum is the page number
  • pageSize is the page size
  • blinkId is the id of this blink

Then we can directly let pageNum=1, pageSize=500, return all the comments directly!
comfortable!

3. Write the code

3.1 Send a request

In Go language net/http, construct a client,

client := &http.Client{
    
    }
reqSpider, err := http.NewRequest("GET", "https://blink-open-api.csdn.net/v1/pc/blink/allComment?pageNum=1&pageSize=400&blinkId=1260435", 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)
}
bodyText, _ := ioutil.ReadAll(respSpider.Body)

3.2 Parsing the data

jsonWe can see that this is standard format data by just looking at the response .

So we Go语言中can use deserialization to stringconvert it into a structurejson格式

	var result BlinkResult
	_ = json.Unmarshal(bodyText, &result)

3.3 Data processing

  • remove duplicate data
func removeBlinkRepByMap(slc []LuckyBlinkPerson) []LuckyBlinkPerson {
    
      //去除重复的元素
	var result []LuckyBlinkPerson           //存放返回的不重复切片
	tempMap := map[LuckyBlinkPerson]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
}
  • Use random seed
func lotteryBlink(totalPerson []LuckyBlinkPerson) LuckyBlinkPerson {
    
      // 抽取中奖选手
	rand.Seed(time.Now().UnixNano())    // 使用随机种子
	index:=rand.Intn(len(totalPerson))  // 生成0到这个列表的长度的一个数字
	return totalPerson[index]  			// 返回中奖选手
}

3.4 Run a lottery

fmt.Println("以下名单进行抽奖:")
for _, item := range luckyBPList {
    
    
	fmt.Println(item.NickName)
}
fmt.Printf("本次抽奖共有 %d 人参与! \n准备抽奖~\n",len(luckyBPList))
for i := 3; i >= 1; i-- {
    
    
	fmt.Println(i)
	time.Sleep(1 * time.Second)
}
fmt.Println("恭喜这位选手中奖~")
luckDog := lotteryBlink(luckyBPList)
fmt.Printf("Blink中奖昵称是:%s 用户名是:%s 评论时间:%s 评论内容:%s\n", luckDog.NickName,luckDog.UserName,luckDog.CreateTime,luckDog.Content)

This lucky one has been drawn! This player has been contacted~
insert image description here

4. Book delivery benefits

As for the benefit of sending books, it will be available every week, so my friends will always have it once 分子!
If you like, you can pay attention~

Guess you like

Origin blog.csdn.net/weixin_45304503/article/details/121198961