初次接触爬虫

#encode=utf-8
import urllib
import urllib2
import time
import os
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
from bs4 import BeautifulSoup
url="http://www.kugou.com"
request=urllib2.Request(url)
result=urllib2.urlopen(request)
soup=BeautifulSoup(result.read())
for i in soup.find_all("div"):
    if i.get("id")=="SongtabContent":
        s=i.find_all("li")
with open(u"c://统计//music.txt","w") as f:
    for i in s:
        f.write(u"歌曲名称为: %s ;" % i.a.select(".songName")[0].text)
        f.write(u"歌曲播放连接为: %s" % i.a.get("href"))
        f.write(u"歌曲播放时间为: %s" % i.a.select(".songTime")[0].text)
        f.write(os.linesep)

posted @ 2019-07-14 21:40  张特别  阅读(215)  评论(0编辑  收藏  举报