使用Beautiful soup 4抓取网易云音乐歌单(http://music.163.com/#/playlist?id=569020058)中的歌曲
Beautiful soup 4的安装,参见:Win7,64位下Python3.5.2,安装Beautiful Soup 4(whl文件)
安装Beautiful soup 4成功后,可以像下面这样导入Beautiful soup 4模块:
>>> import bs4>>>
抓取歌曲的代码
#encoding=utf8import requestsfrom bs4 import BeautifulSoupheaders = { 'Referer':'http://music.163.com/', 'Host':'music.163.com', 'User-Agent': 'Mozilla/5.0 (X11; linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }play_url = 'http://music.163.com/playlist?id=569020058's = requests.session()s = BeautifulSoup(s.get(play_url,headers = headers).content)main = s.find('ul',{'class':'f-hide'})for music in main.find_all('a'): PRint('{} : {}'.format(music.text,music['href']))抓取结果:
新闻热点
疑难解答