介紹
本篇文章主要介紹如何爬取麥子學院的課程信息(本爬蟲仍是單線程爬蟲),在開始介紹之前,先來看看結果示意圖

怎么樣,是不是已經躍躍欲試了?首先讓我們打開麥子學院的網址,然后找到麥子學院的全部課程信息,像下面這樣

這個時候進行翻頁,觀看網址的變化,首先,第一頁的網址是 http://www.maiziedu.com/course/list/, 第二頁變成了 http://www.maiziedu.com/course/list/all-all/0-2/, 第三頁變成了 http://www.maiziedu.com/course/list/all-all/0-3/ ,可以看到,每次翻一頁,0后面的數字就會遞增1,然后就有人會想到了,拿第一頁呢?我們嘗試著將 http://www.maiziedu.com/course/list/all-all/0-1/ 放進瀏覽器的地址欄,發現可以打開第一欄,那就好辦了,我們只需要使用 re.sub() 就可以很輕松的獲取到任何一頁的內容。獲取到網址鏈接之后,下面要做的就是獲取網頁的源代碼,首先右擊查看審查或者是檢查元素,就可以看到以下界面

找到課程所在的位置以后,就可以很輕松的利用正則表達式將我們需要的內容提取出來,至于怎么提取,那就要靠你自己了,嘗試著自己去找規律才能有更大的收獲。如果你實在不知道怎么提取,那么繼續往下,看我的源代碼吧
實戰源代碼
# coding=utf-8 import re import requests import sys reload(sys) sys.setdefaultencoding("utf8") class spider(): def __init__(self): print "開始爬取內容。。。" def changePage(self, url, total_page): nowpage = int(re.search('/0-(/d+)/', url, re.S).group(1)) pagegroup = [] for i in range(nowpage, total_page + 1): link = re.sub('/0-(/d+)/', '/0-%s/' % i, url, re.S) pagegroup.append(link) return pagegroup def getsource(self, url): html = requests.get(url) return html.text def getclasses(self, source): classes = re.search('<ul class="zy_course_list">(.*?)</ul>', source, re.S).group(1) return classes def geteach(self, classes): eachclasses = re.findall('<li>(.*?)</li>', classes, re.S) return eachclasses def getinfo(self, eachclass): info = {} info['title'] = re.search('<a info['people'] = re.search('<p class="color99">(.*?)</p>', eachclass, re.S).group(1) return info def saveinfo(self, classinfo): f = open('info.txt', 'a') for each in classinfo: f.writelines('title : ' + each['title'] + '/n') f.writelines('people : ' + each['people'] + '/n/n') f.close() if __name__ == '__main__': classinfo = [] url = 'http://www.maiziedu.com/course/list/all-all/0-1/' maizispider = spider() all_links = maizispider.changePage(url, 30) for each in all_links: htmlsources = maizispider.getsource(each) classes = maizispider.getclasses(htmlsources) eachclasses = maizispider.geteach(classes) for each in eachclasses: info = maizispider.getinfo(each) classinfo.append(info) maizispider.saveinfo(classinfo)以上代碼并不難懂,基本就是正則表達式的使用,然后直接運行就可以看到開頭我們的截圖內容了,由于這是單線程爬蟲,所以運行速度感覺有點慢,接下來還會繼續更新多線程爬蟲。
應小伙伴們的要求,下面附上requests爬蟲庫的安裝和簡單示例
首先安裝pip包管理工具,下載get-pip.py. 我的機器上安裝的既有python2也有python3。
安裝pip到python2:
python get-pip.py
安裝到python3:
python3 get-pip.py
pip安裝完成以后,安裝requests庫開啟python爬蟲學習。
安裝requests
pip3 install requests
我使用的python3,python2可以直接用pip install requests.
入門例子
import requestshtml=requests.get("http://gupowang.baijia.baidu.com/article/283878")html.encoding='utf-8'print(html.text) 第一行引入requests庫,第二行使用requests的get方法獲取網頁源代碼,第三行設置編碼格式,第四行文本輸出。
把獲取到的網頁源代碼保存到文本文件中:
import requestsimport oshtml=requests.get("http://gupowang.baijia.baidu.com/article/283878")html_file=open("news.txt","w")html.encoding='utf-8'print(html.text,file=html_file)新聞熱點
疑難解答