用面向物件的思想程式設計思想使用requests、lxml模組爬取酷我音樂榜單的音樂,並用json格式匯出檔案。
阿新 • • 發佈:2018-12-13
首先匯入響應的模組:
import requests
from lxml import etree
import json
然後新建一個class類,並建立需要的例項:
class KuwoSpider: def __init__(self): self.start_url = "http://www.kuwo.cn/bang/index" # 1.start_url self.headers = {"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"}
然後寫一個run函式來實現主要邏輯:
def run(self): # 實現主要邏輯
# 1.start_url
# 2.傳送請求,獲取響應
html_str = self.parse_url()
# 3.提取資訊
content_list = self.get_content_list(html_str)
# 4.儲存
self.save_content_list(content_list)
接下來就可以根據根據主要邏輯來實現相應的步驟:
def parse_url(self): # 2.傳送請求,獲取響應 response = requests.get(self.start_url, headers=self.headers) return response.content.decode() def get_content_list(self, html_str): # 3.提取資訊 html = etree.HTML(html_str) div_list = html.xpath("//ul[@class='listMusic']/li") print(div_list) content_list = [] for div in div_list: item = {} item["order"] = div.xpath(".//p[@class='num']/text()")[0] item["song_name"] = div.xpath("./div[@class='name']/a/text()")[0] item["song_url"] = div.xpath("./div[@class='name']/a/@href")[0] item["songer"] = div.xpath("./div[@class='artist']/a/text()")[0] item["heatValue"] = div.xpath(".//div[@class='heatValue']/@style") item["heatValue"] = item["heatValue"][0].replace("width:", "") content_list.append(item) return content_list def save_content_list(self, content_list): # 4.儲存 with open("files/kuwo.txt", "w", encoding="utf-8") as f: for content in content_list: f.write(json.dumps(content, ensure_ascii=False, indent=2)) f.write("\n") print("儲存成功")
最後例項化類並呼叫類中的run函式:
if __name__ == '__main__':
kuwo = KuwoSpider()
kuwo.run()
這樣,執行這個程式便可以將榜單中的你想要的資訊爬取出來:
{ "order": "1", "song_name": "盜將行", "song_url": "http://www.kuwo.cn/yinyue/53959930?catalog=yueku2016", "songer": "花僮", "heatValue": "92%" } { "order": "2", "song_name": "走馬", "song_url": "http://www.kuwo.cn/yinyue/54002473?catalog=yueku2016", "songer": "摩登兄弟", "heatValue": "91%" } { "order": "3", "song_name": "作曲家", "song_url": "http://www.kuwo.cn/yinyue/53996611?catalog=yueku2016", "songer": "劉郡格", "heatValue": "98%" } ... ...
上面的資料就是我們所想要爬取的資訊,在這裡我只擷取前三個資料。