圆月山庄资源网 Design By www.vgjia.com

本文为大家分享了python爬取酷狗音乐排行榜的具体代码,供大家参考,具体内容如下

#coding=utf-8
from pymongo import MongoClient
import time 
import requests 
from lxml import etree 
 
client = MongoClient()      #连接mongo
hello = client.hello       #连接数据库
user = hello.song         #连接表
 
headers = { 
  'User-Agent': 'Mozilla/5.0 (Android 6.0; Nexus 5 Build/MRA58N)  AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36'} 
 
def get_info(url): 
  '''
  get源码,encode,解析,xpath,保存 
  '''
  response = requests.get(url, headers=headers) 
  response = response.text.encode('utf-8') 
  selector = etree.HTML(response) 
  soup = selector.xpath('//*[@class="pc_temp_songlist "]/ul//li/a/text()') 
 
  #保存到本地
  # with open('aa.txt','a') as f:
    # for i in soup:
      # f.write(i.encode('utf-8') + '\n')
 
  #存入数据库
  for i in soup:
    user.insert({'song': i})
 
if __name__ == '__main__': 
  urls = ['http://www.kugou.com/yy/rank/home/{}-8888.html?from=rank'.format(str(i)) for i in range(1, 24)] 
  for url in urls: 
    print(url) 
    get_info(url)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python爬取酷狗音乐排行榜,python爬取酷狗音乐,python爬取音乐排行榜

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com