Home  >  Article  >  Backend Development  >  NetEase Cloud music review crawling

NetEase Cloud music review crawling

jacklove
jackloveOriginal
2018-06-11 23:42:122726browse

# coding=gbk
import requests
import json
c='网易云爬虫实战一'
print(c)
music_url = 'https://music.163.com/#/song?id=28815250'
id = music_url.split('=')[1]
# print(id)
url = 'https://music.163.com/weapi/v1/resource/comments/R_SO_4_%s?csrf_token=7e19029fe28aa3e09cfe87e89d2e4eeb' %(id)


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    'Referer': 'https://music.163.com/song?id=%s' %(id),
    'Origin': 'https://music.163.com',
    }


formdata = {
    'params': 'AoF/ZXuccqvtaCMCPHecFGVPfrbtDj4JFPJsaZ3tYn9J+r0NcnKPhZdVECDz/jM+1CpA+ByvAO2J9d44B/MG97WhjmxWkfo4Tm++AfyBgK11NnSbKsuQ5bxJR6yE0MyFhU8sPq7wb9DiUPFKs2ulw0GxwU/il1NS/eLrq+bbYikK/cyne90S/yGs6ldxpbcNd1yQTuOL176aBZXTJEcGkfbxY+mLKCwScAcCK1s3STo=',
    'encSecKey': '365b4c31a9c7e2ddc002e9c42942281d7e450e5048b57992146633181efe83c1e26acbc8d84b988d746370d788b6ae087547bace402565cca3ad59ccccf7566b07d364aa1d5b2bbe8ccf2bc33e0f03182206e29c66ae4ad6c18cb032d23f1793420ceda05e796401f170dbdb825c20356d27f07870598b2798f8d344807ad6f2',
    }


response = requests.post(url, headers = headers, data = formdata)
messages = json.loads(response.text)


data_list=[]
data={}
for message in messages['hotComments']:
	data['nickname']=message['user']['nickname']
	data['content']=message['content']
	data_list.append(data)
	data={}
#print(data_list)
for i in data_list:
	c = '    '+i['nickname']+':'+i['content']
	print('\n\n'+c.replace('\n',''))


##Summary:

1. The "# in the first line coding=gbk" means that you can enter text strings in the text editor.

2. The split() function in "id = music_url.split('=')[1]" means to group elements, in this example it is "

https://music.163.com /#/song?id=", "28815250"

3. The HTML text obtained by the requests module needs to be converted into Python readable using the json.loads() method text, otherwise an error will be reported. This does not happen in jupyter notebook.

4. The replace() function can remove elements from the string. In this example, the newline character is changed to empty.

The final display result is as follows:


##This article introduces the relevant content of NetEase Cloud music review crawling, please Follow php Chinese website.

Related recommendations:

Simple PHP MySQL paging class

Two tree array constructors without recursion


Convert HTML to Excel, and realize printing and downloading functions

The above is the detailed content of NetEase Cloud music review crawling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn