In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize the raffle function in the comment area based on Python". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to realize the raffle function in the comment area based on Python" together!
1. Analysis Comment Interface
First, we need to find the "interface" for review data, which is the request for review data from the website.
Open a raffle article, enter "Developer Mode"(press F12 or right-click check), select the Network option, and "Refresh" the article page to resend the request. Observe the request sent by the page in the right toolbar, analyze the request one by one, and determine the request for comment according to the response content.
You can view the full "Request" in the Headers column.
https://blog.csdn.net/phoenix/web/v1/comment/list/121888905? page=1&size=3&commentId=
2. Get comment data
With the interface in place, we use the requests module to send requests for comment data
Notice the two parameters in the request: page and size
1.page indicates page number, here pass 1
2. size indicates how many pieces of data are displayed per page, here pass 100 (write a few comments)
import requests#Comment request link url = 'blog.csdn.net/phoenix/web/v1/comment/list/121888905? page=1&size=100&commentId='#request header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" }#Get request data for comments (json string) page = requests.get(url, headers=header)print(page.text)3. Filter comments User
nickName field stores the name of the commenting user. We divide the obtained data into arrays according to, and then filter out the data containing nickName to obtain the names of all commenting users. The core code is as follows
titles = page.text.split (',') #Get each field names = [] #Array of commenter names for t in titles: #Get all commenters field if 'nickName' in t and '"nickName":"三日wyx"' not in t: name = t[12:len(t)-1] #intercept commenter name names.append(name) #Add a commenter name to the array print(names)
In addition to the above, you can also use regular expressions to match the user's name. The core code is as follows
#Get all reviewer names = re.findall ('nickName ":"([^"]*)",', page.text)pond = [] #Prize pool for name in names: if 'wyx' not in name: pond.append(name) #Place a participant in the prize pool 4. Draw lucky viewers
Use random.choice function to generate random users, as lucky viewers, the core code is as follows
#Randomly generated lucky audience star = random.choice(pond)print ('number of participants: %d' % len(pond))print ('lucky audience: %s' % star)5. Full source code 5.1 String interception method
Filter data by "intercepting string" method, which is more accurate and suitable for most scenes
import requestsimport random#Comment requestlinkurl = 'blog.csdn.net/phoenix/web/v1/comment/list/121888905? page=1&size=100&commentId='#request header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" }#Get request data for comments (json string) page = requests.get(url, headers=header)titles = page.text.split (',') #Get names for each field = [] #Array of commenter names for t in titles: #Get all commenters field if 'nickName' in t and '"nickName":"三日wyx"' not in t: name = t[12:len(t)-1] #intercept commenter name names.append(name) #Add commenter name to array #Randomly generate lucky audience star = random.choice(pond)print ('Lucky audience: %d' % len(names))print ('Lucky audience: %s' % star)5.2 Regular matching method
Use the "regular match" method to filter the data, there is a certain error (the username cannot contain ")
import requestsimport randomimport re#Comment requestlinkurl = 'blog.csdn.net/phoenix/web/v1/comment/list/121888905? page=1&size=100&commentId='#request header = { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" }#Get request data for reviews (json string) page = requests.get(url, headers=header)#Get all reviewer names = re.findall ('nickName ":"([^"]*)",', page.text)pond = [] #Prize pool for name in names: if 'wyx' not in name: pond.append(name) #Place participants in the prize pool #Randomly generate lucky audience star = random.choice(pond)print ('Number of participants: %d' % len(pond))print ('lucky audience: %s' % star)5.3 Execution Results
Test Result 1
E:\data\PrCharm\pythonProject\venv\Scripts\python.exe E:/data/PrCharm/pythonProject/award.py
Number of participants: 15
Lucky audience: Willing Kaka
Process finished with exit code 0
Test Result 2
E:\data\PrCharm\pythonProject\venv\Scripts\python.exe E:/data/PrCharm/pythonProject/award.py
Number of participants: 15
Lucky Audience: Xiao Hui_Super
Process finished with exit code 0
Thank you for your reading. The above is the content of "How to realize the raffle function of comment area based on Python". After studying this article, I believe everyone has a deeper understanding of how to realize the raffle function of comment area based on Python. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 240
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.