通俗易懂的分析如何用Python實現一隻小爬蟲,爬取拉勾網的職位資訊

來源:互聯網
上載者:User

標籤:city   .com   start   utf-8   sea   arch   use   方法   經驗   

原始碼:https://github.com/nnngu/LagouSpider

效果預覽

思路

1、首先我們開啟拉勾網,並搜尋“java”,顯示出來的職位資訊就是我們的目標。

2、接下來我們需要確定,怎樣將資訊提取出來。

  • 查看網頁原始碼,這時候發現,網頁原始碼裡面找不到職位相關資訊,這證明拉勾網關於職位的資訊是非同步載入的,這也是一種很常用的技術。

  • 非同步載入的資訊,我們需要藉助 chrome 瀏覽器的開發人員工具進行分析,開啟開發人員工具的方法如下:

  • 點擊Nerwork進入網路分析介面,這時候是一片空白,重新整理一下介面就可以看到一系列的網路請求了。

  • 前面我們說到,拉勾網關於職位的資訊是非同步載入的,那麼在這一系列的網路請求中,必定有某個請求發送給伺服器,響應回來的是職位資訊。

  • 正常情況下,我們可以忽略css,圖片等類型的請求,關注點放在XHR這種類型請求上,

一共4個XHR類型的請求,我們逐個開啟對比,分別點擊Preview就能看到它們響應的內容。

發現第一個請求就是我們要找的。

點擊Headers,查看一下請求參數。如:

到此,我們可以確定city參數就是城市,pn參數就是頁數,kd參數就是搜尋索引鍵。

接下來開始寫代碼了。

代碼

代碼分成四個部分,便於後期維護。

1、基本 https 請求https.py

這部分對 requests 包進行了一些封裝,部分代碼如下:

# -*- coding: utf-8 -*-from src.setting import IP, UAimport requests, randomimport loggingclass Http:    '''    http請求相關的操作    '''    def __init__(self):        pass    def get(self, url, headers=None, cookies=None, proxy=None, timeOut=5, timeOutRetry=5):        '''        擷取網頁源碼        url: 網頁連結        headers: headers        cookies: cookies        proxy: 代理        timeOut: 請求逾時時間        timeOutRetry: 逾時重試次數        return: 源碼        '''        if not url:            logging.error('GetError url not exit')            return 'None'                    # 這裡只展示了一部分代碼        # 完整代碼已上傳到Github

這裡只展示了一部分代碼,完整代碼已上傳到Github

2、代碼主邏輯部分main.py

這部分的程式邏輯,如下:

  • 擷取職位資訊
def getInfo(url, para):    """    擷取資訊    """    generalHttp = Http()    htmlCode = generalHttp.post(url, para=para, headers=headers, cookies=cookies)    generalParse = Parse(htmlCode)    pageCount = generalParse.parsePage()    info = []    for i in range(1, 3):        print('第%s頁' % i)        para['pn'] = str(i)        htmlCode = generalHttp.post(url, para=para, headers=headers, cookies=cookies)        generalParse = Parse(htmlCode)        info = info + getInfoDetail(generalParse)        time.sleep(2)    return info
  • 對資訊進行儲存
def processInfo(info, para):    """    資訊儲存    """    logging.error('Process start')    try:        title = '公司名稱\t公司類型\t融資階段\t標籤\t公司規模\t公司所在地\t職位類型\t學曆要求\t福利\t薪資\t工作經驗\n'        file = codecs.open('%s職位.xls' % para['city'], 'w', 'utf-8')        file.write(title)        for p in info:            line = str(p['companyName']) + '\t' + str(p['companyType']) + '\t' + str(p['companyStage']) + '\t' + \                   str(p['companyLabel']) + '\t' + str(p['companySize']) + '\t' + str(p['companyDistrict']) + '\t' + \                   str(p['positionType']) + '\t' + str(p['positionEducation']) + '\t' + str(                p['positionAdvantage']) + '\t' + \                   str(p['positionSalary']) + '\t' + str(p['positionWorkYear']) + '\n'            file.write(line)        file.close()        return True    except Exception as e:        print(e)        return None

3、資訊解析部分parse.py

這部分針對伺服器返回的職位資訊的特點,進行解析,如下:

class Parse:    '''    解析網頁資訊    '''    def __init__(self, htmlCode):        self.htmlCode = htmlCode        self.json = demjson.decode(htmlCode)        pass    def parseTool(self, content):        '''        清除html標籤        '''        if type(content) != str: return content        sublist = ['<p.*?>', '</p.*?>', '<b.*?>', '</b.*?>', '<div.*?>', '</div.*?>',                   '</br>', '<br />', '<ul>', '</ul>', '<li>', '</li>', '<strong>',                   '</strong>', '<table.*?>', '<tr.*?>', '</tr>', '<td.*?>', '</td>',                   '\r', '\n', '&.*?;', '&', '#.*?;', '<em>', '</em>']        try:            for substring in [re.compile(string, re.S) for string in sublist]:                content = re.sub(substring, "", content).strip()        except:            raise Exception('Error ' + str(substring.pattern))        return content                # 這裡只展示了一部分代碼        # 完整代碼已上傳到Github

這裡只展示了一部分代碼,完整代碼已上傳到Github

4、配置部分setting.py

這部分加入 cookies 的原因是為了應對拉勾網的反爬,長期使用需要進行改進,進行動態 cookies 擷取

# -*- coding: utf-8 -*-# headersheaders = {    'Host': 'www.lagou.com',    'Connection': 'keep-alive',    'Content-Length': '23',    'Origin': 'https://www.lagou.com',    'X-Anit-Forge-Code': '0',    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',    'Accept': 'application/json, text/javascript, */*; q=0.01',    'X-Requested-With': 'XMLHttpRequest',    'X-Anit-Forge-Token': 'None',    'Referer': 'https://www.lagou.com/jobs/list_java?city=%E5%B9%BF%E5%B7%9E&cl=false&fromSearch=true&labelWords=&suginput=',    'Accept-Encoding': 'gzip, deflate, br',    'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7'}
測試

運行結果:

爬取結束後,在src目錄下就可以看到爬蟲爬取到的資料。

到此,拉勾網的職位資訊抓取就完成了。完整代碼已經上傳到我的Github

通俗易懂的分析如何用Python實現一隻小爬蟲,爬取拉勾網的職位資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.