聲明:本文僅作為學習愛好者編寫,請勿商業和惡意攻擊源網站,本文所有解釋權歸作者
本文沒有使用爬蟲框架,僅用了三個Python的常用庫
本文適合新手參考,文章里面有大量注釋為理解提供便利
# 爬喜馬拉雅
import requests
from lxml import etree
import os
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
'Referer':'https://www.ximalaya.com/'
}
# 設置url 我們先搞第一頁,注意默認p1是省略的 總歌單 單歌曲
# https://www.ximalaya.com/youshengshu/14968275/83332135
# 如果要爬取別的區塊的內容,則修改下面的url就可以了
url = "https://www.ximalaya.com/youshengshu/p1/"
# 列表頁函數
def listing(url):
#直接請求
response = requests.get(url=url, headers=headers).text
# 生成解析對象
etrees = etree.HTML(response)
# 解析頁面 獲取當前頁的歌曲地址
page_listing_url = etrees.xpath('//div[@class="sound-list _yo5_"]/ul/li/div[@class="text _yo5_"]/a/@href')
# 獲取歌曲名稱
page_listing_name = etrees.xpath('//div[@id="anchor_sound_list"]/div/ul/li/div[@class="text _yo5_"]/a/span/text()')
# 獲取總的名字
all_name = etrees.xpath('//div[@class="info _J460"]/h1/text()')[0]
# 遍歷歌曲url不是傳到詳情頁 而是傳到外鏈頁 https://link.hhtjim.com/ximalaya/203837977.mp3
file_path = "./%s/"%all_name
if not os.path.exists(file_path):
os.mkdir(file_path)
for i,j in enumerate(page_listing_url):
for k,v in enumerate(page_listing_name):
if i == k:
# 分割i ,拿出歌曲id
song_id = j.split("/")[-1]
# 拼接路由
url = "https://link.hhtjim.com/ximalaya/" + song_id + ".mp3"
# 傳給外鏈頁url,返回歌曲內容
content = linking(url)
with open(file_path + v + ".mp3","wb") as f:
f.write(content)
print("%s,%s下載成功"%(all_name,v))
# 獲取下一頁的列表,有下一頁則遞歸
next_url = etrees.xpath('//div[@class="pagination _yo5_"]/nav/ul/li[@class="page-next page-item _dN2"]/a/@href')
if next_url:
# 拼接路由
url = "https://www.ximalaya.com" + next_url[0]
# 遞歸調用
listing(url)
else:
pass
# 外鏈頁函數
def linking(url):
response = requests.get(url=url,headers=headers).content
return response
# 分類首頁函數
def category_index(url):
# 發送請求
response = requests.get(url=url,headers=headers).text
# 生成解析對象
etrees = etree.HTML(response)
# 解析頁面 獲取當前頁的歌單地址
# page_url_list = etrees.xpath('//div[@class="content"]/ul/li/div/a[@class="album-title line-1 lg bold _qie"]/@href')
page_url_list = etrees.xpath('//div[@class="content"]/ul/li/div/a[@class="album-title line-1 lg bold _qie"]/@href')
print(page_url_list)
# page_name_list = etrees.xpath('//div[@class="content"]/ul/li/div/a/span/text()')
# 獲取之后遍歷,直接把url傳到列表頁
for i in page_url_list:
# 拼接url傳值 /youshengshu/25407248/ https://www.ximalaya.com/youshengshu/25407248/
url = "https://www.ximalaya.com" + i
listing(url)
# 判斷是否還有下一頁 /youshengshu/p2/
next_url = etrees.xpath('//div[@class="pagination-wrap"]/nav/ul/li[@class="page-next page-item _dN2"]/a/@href')
if next_url:
# 拼接路由
url = "https://www.ximalaya.com" + next_url[0]
# 遞歸調用
category_index(url)
else:
pass
# 調用分類首頁函數
category_index(url)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
