#!/usr/bin/python
# -*- coding: utf-8 -*-
from urllib import request
import re
# 获取豆瓣网音乐人的专辑名称和喜欢的人数
class Spider():
url = 'https://music.douban.com/artists/genre_page/6/'
root_pattern = '<div style="width:100%;" class="11">([\s\S]*?)</div>'
name_pattern = '<a>([\s\S]*?)</a>'
number_pattern = '<div class="p1">([\s\S]*?)</div>'
#获取源文档
def __content(self):
r = request.urlopen(Spider.url)
htmls = r.read()
htmls = str(htmls, encoding='utf-8')
return htmls
#对源文档进行提炼,提取专辑名和喜欢的人数
def __analisys(self,htmls):
root_html = re.findall(Spider.root_pattern, htmls)
anchors = []
for html in root_html:
name = re.findall(Spider.name_pattern, html)
number = re.findall(Spider.number_pattern, html)
anchor = {'name': name, 'number': number}
anchors.append(anchor)
print(anchors)
def go(self):
htmls = self.__content()
self.__analisys(htmls)
#调用
spider = Spider()
spider.go()