Beautiful Soup標準庫是一個可以從HTML/XML檔案中提取資料的Python庫,它能夠透過你喜歡的轉換器實現慣用的文件導航,查找,修改文件的方式,Beautiful Soup將會節省數小時的工作時間。 pymongo標準庫是MongoDb NoSql資料庫與python語言之間的橋樑,透過pymongo將資料保存到MongoDb中。結合使用這兩者來爬去喜馬拉雅電台的資料...
Beautiful Soup支援Python標準庫中的HTML解析器,也支援一些第三方的解析器,其中一個是 lxml。本文所使用的就是lxml,對於這個的安裝,請看python 3.6 lxml標準函式庫lxml的安裝及etree的使用注意
同時,本文使用了XPath來解析我們想要的部分,對於XPath與Beautiful Soup的介紹與使用請看Beautiful Soup 4.4.0 文件XPath 簡介
本文涉及到的Beautiful Soup與XPath的知識不是很深,看看官方文件就能理解,而且我還加上了註釋...
對於pymongo標準函式庫,我就不多扯淡了,詳情請看python標準庫之pymongo模組次體驗
有時候,我們需要判斷目前向伺服器發出請求的客戶端的類型,也就是通常所說的User-Agent,簡稱UA,我們在瀏覽網頁時所使用的瀏覽器就是UA的一種,換言之,UA就是瀏覽器,在HTTP協定中,透過User-Agent請求頭說明使用者瀏覽器的類型,操作系統,瀏覽器內核等資訊的標識。透過這個標識,用過所造訪的網站可以顯示不同的版本,從而為使用者提供更好的體驗或進行資訊統計。而有些網站正式利用UA來防止駭客或是像我們這樣無聊的人來爬去網站的資料資訊。
因此,本文程式碼首先就把所有的UA都給列取出來,以方便後續的爬取工作。
好了,下面來明確下我們要爬取得數據是什麼:
#我們需要的是圖片的鏈接,alt等
接著我們點擊圖片連結之後,取得裡面的詳情,如果有些電台是多頁的,那麼我們用過xpath來依序造訪。同時我們取得頁面中專輯裡的聲音模組的sound_id...
程式如下:
import random import requests from bs4 import BeautifulSoup import json from lxml import etree import pymongo clients = pymongo.MongoClient("localhost", 27017) db = clients["XiMaLaYa"] collection_1 = db["album"] collection_2 = db["detail"] UA_LIST = [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1", "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3", "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24" ] headers1 = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6', 'Cache-Control': 'max-age=0', 'Proxy-Connection': 'keep-alive', 'Upgrade-Insecure-Requests': '1', 'User-Agent': random.choice(UA_LIST) # User_agence表示用户代理 } headers2 = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6', 'Cache-Control': 'max-age=0', 'Proxy-Connection': 'keep-alive', 'Referer': 'http://www.ximalaya.com/dq/all/2', 'Upgrade-Insecure-Requests': '1', 'User-Agent': random.choice(UA_LIST) } # Beautiful库用来处理XML和HTML... # 主要就是利用BeautifulSoup模块来处理requests模块获取的Html源码 # 利用lxml模块将html源码解析成树结构,xpath来处理树节点. def get_url(): start_urls = ["http://www.ximalaya.com/dq/all/{}".format(num) for num in range(1,85)] # start_urls = ["http://www.ximalaya.com/dq/all/1"] for start_url in start_urls: html = requests.get(start_url, headers=headers1).text soup = BeautifulSoup(html, "lxml") # 使用lxml来处理 for item in soup.find_all(class_="albumfaceOutter"): # 解析并查找xml节点 content = { 'href': item.a["href"], 'title': item.img['alt'], 'img_url': item.img['src'] } collection_1.insert(content) # another(item.a["href"]) print('写入完成...') # 进入电台具体页面 http://www.ximalaya.com/15836959/album/303085,并处理分页录音... def another(url): html = requests.get(url, headers=headers1).text # / :表示从根节点选取.... # // :表示匹配选择的当前节点选择文档中的节点,而不考虑他们的位置... ifanother = etree.HTML(html).xpath('//div[@class="pagingBar_wrapper"]/a[last()-1]/@data-page') # 页面链接地址 ifanother是list类型... if len(ifanother): # 判断一个video的录音是否分割成了多页.... num = ifanother[0] # 获取页面数... print('本频道保存在' + num + '个页面') for n in range(1, int(num)): url2 = url + '?page={}'.format(n) get_m4a(url2) get_m4a(url) # 获取分页录音页面的详细数据... def get_m4a(url): html = requests.get(url, headers=headers2).text numlist = etree.HTML(html).xpath('//div[@class="personal_body"]/@sound_ids')[0].split(',') for i in numlist: murl = 'http://www.ximalaya.com/tracks/{}.json'.format(i) html = requests.get(murl, headers=headers1).text dic = json.loads(html) collection_2.insert(dic) if __name__ == "__main__": get_url()
以上是關於python標準庫Beautiful Soup與MongoDb爬喜馬拉雅電台的總結的詳細內容。更多資訊請關注PHP中文網其他相關文章!