寫在前面
從今天開始的幾篇文章,我將就國內目前比較主流的一些在線學習平台數據進行抓取,如果時間充足的情況下,會對他們進行一些簡單的分析,好了,平台大概有51CTO學院
,CSDN學院
,網易雲課堂
,慕課網
等平台,數據統一抓取到mongodb
里面,如果對上述平台造成了困擾,請見諒,畢竟我就抓取那么一小會的時間,不會對服務器有任何影響的。
1. 目標網站
今天的目標網站是 http://edu.51cto.com/courselist/index.html?edunav
數據量大概在1W+,還不錯
2. 分析頁面需要的信息
下圖標注的框框,就是我們需要的信息了
如果查看源碼,我們還能得到其他有價值的隱藏信息,也同時的抓取到,另外,今天的主題不是下載圖片,所以針對課程縮略圖,我只保留一個圖片鏈接到mongodb
里面,就不做單獨的處理了。
在開發者工具中,繼續檢索有用信息。發現一個獨家
這個信息看似有用唉,可以做保留。
3. 分析爬取方式
分析完畢就是要爬取操作了,看一下這個網站是否是ajax動態加載的,如果不是,那么就采用最笨的辦法爬取。
查閱網站源代碼之后,發現沒有異步數據。
采用URL拼接的方式爬取即可。
URL規律如下,因為數據相對變化不大,末尾的頁碼是417,所以URL直接生成就可以了。
https://edu.51cto.com/courselist/index-p2.html
https://edu.51cto.com/courselist/index-p3.html https://edu.51cto.com/courselist/index-p4.html https://edu.51cto.com/courselist/index-p5.html ... http://edu.51cto.com/courselist/index-p417.html
今天主要使用requests-html
這個庫。
我們拿51cto學院
,完整的練個手。
from requests_html import HTMLSession BASE_URL = "http://edu.51cto.com/courselist/index-p{}.html" def get_content(): session = HTMLSession() r = session.get(BASE_URL) print(r.html) if __name__ == '__main__': get_content()
使用上面的代碼,就能快速的獲取到一個請求的響應了。
繼續編寫下面幾行代碼之后,你不得不驚嘆,我去~,數據都獲取到了!
print(r.html)
print(r.html.links)
print(r.html.absolute_links) # 獲取所有的絕對地址 print(r.html.find('.cList',first=True)) # 獲取class=cList的第一個標簽 c_list = r.html.find('.cList',first=True) print(c_list.text)
當然這些對咱來說還是遠遠不夠的,畢竟我們要把他寫入mongodb里面
上面的只是叫你對這個庫有一個基本的認知,更多的資料你可以去他的教程網站查閱
http://html.python-requests.org/
4. 分析爬取方式
看一下異步方式,異步的出現可以為我們的爬蟲加速
這個地方有一個你一定要注意的,我寫這篇文章的時候,requests-html
是從github下載之后,更新的本次,你如果之前使用pip進行安裝,那么異步應該是沒有更新上去的。
好了,接下來我們實現一下異步,可能由於作者認為異步目前不是很穩定,所以我查閱了一下他的源碼,然后實現了如下代碼,寫的不好,請見諒~
下面的代碼,注意看模塊的區別,以及核心的異步函數
async def get_html(): for i in range(1,3): r = await asession.get(BASE_URL.format(i)) # 異步等待 get_item(r.html) if __name__ == '__main__': result = asession.run(get_html)
from requests_html import AsyncHTMLSession # 導入異步模塊 asession = AsyncHTMLSession() BASE_URL = "http://edu.51cto.com/courselist/index-p{}.html" async def get_html(): for i in range(1,418): r = await asession.get(BASE_URL.format(i)) # 異步等待 get_item(r.html)
def get_item(html): c_list=html.find('.cList',first=True) if c_list: items=c_list.find('.cList_Item') for item in items: title=item.find("h3",first=True).text href=item.find('h3>a',first=True).attrs["href"] class_time=item.find("div.course_infos>p:eq(0)",first=True).text study_nums = item.find("div.course_infos>p:eq(1)", first=True).text stars=item.find("div.course_infos>div",first=True).attrs['val'] course_target=item.find(".main>.course_target",first=True).text if item.find(".price>h4", first=True): price = item.find(".price>h4", first=True).text elif item.find(".price>span", first=True): price = item.find(".price>span", first=True).text dict = { "title": title, "href": href, "class_time": class_time, "study_nums": study_nums, "stars": stars, "course_target": course_target, "price": price } print(dict) else: print("數據解析失敗")
if __name__ == '__main__':
result = asession.run(get_html)
代碼運行之后,控制台就會輸出相應的內容,上述代碼中有個地方用到了大量的解析HTML,這個你搜索一下官方文檔就可以看明白,不進行過多的解釋。
5. 寫入到mongodb里面
這部分代碼就非常非常簡單了
結果如下
實際的爬取過程中,也沒有發現反爬蟲的一些限制,不過咱畢竟是為了研究一下requests-html
的用法,所以只能對51CTO網站說一句多有得罪,罪過罪過。