1.爬取目標
這次爬蟲爬取的信息是騰訊官方招聘網站上的首頁下的熱招職位,如下圖所示
2.爬取步驟
進入該頁面下,觀察該頁面,我們爬取的信息就是下面我標出的信息頁,信息頁下面就是翻頁區,不斷翻頁發現這些網頁的鏈接后面有一定的規則
每頁的鏈接:http://hr.tencent.com/position.php?&start=?#a 注:?就是改變的位置
使用requests模塊獲取頁面信息:
1 # 請求獲取網站頁面的信息(html代碼) 網址:http://hr.tencent.com/position.php 2 def load_request(number, items): 3 # User-Agent: 請求報頭,用來把程序偽裝成瀏覽器 4 headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"} 5 # 使用requests的get請求服務器獲取信息 6 request = requests.get('http://hr.tencent.com/position.php?&start='+str(number)+"#a", headers=headers) 7 # 取出返回信息的文本信息(html代碼) 8 html = request.text
使用BeautifulSoup解析庫對獲取的頁面代碼進行解析,獲取的頁面信息其實就是上面第二張圖的一頁,打開瀏覽器的檢查觀察頁面HTML代碼的規則,每一條職位信息都在類名為even或odd的td標簽內,先用select方法提取每一條信息的職位信息,再循環取每一條信息里的特定信息:
select方法使用: https://www.cnblogs.com/yizhenfeng168/p/6979339.html(轉載)
1 # 網站代碼解析 2 def parser(html, items): 3 # soup是一個BeautifulSoup對象,把HTML代碼轉換成樹形結構存儲在soup中 4 soup = BeautifulSoup(html, 'html.parser') 5 # 使用BeautifulSoup中的select方法提取HTML代碼中的信息 6 even = soup.select('.even') 7 odd = soup.select('.odd') 8 str = odd + even # 字符串拼接 9 for item in str: # for循環迭代(遍歷) 10 # _item為一個字典,存儲一個工作職位的信息 11 _item = {} 12 # 職位名稱 13 _item['name_of_work'] = item.select('td')[0].get_text() 14 # 職位鏈接 15 _item['link_of_work'] = 'http://hr.tencent.com' + item.select('td a')[0].attrs['href'] 16 # 職位類別 17 _item['category_of_work'] = item.select('td')[1].get_text() 18 # 工作地點 19 _item['where_of_work'] = item.select('td')[3].get_text() 20 # 發布時間 21 _item['time_of_release'] = item.select('td')[4].get_text() 22 # 所需人數 23 _item['number_of_person'] = item.select('td')[2].get_text() 24 # 把以上信息添加到列表中 25 items.append(_item)
最后寫主函數,把爬取的信息以json格式存儲,程序的所有代碼如下:

1 # __author__ = 'wyb' 2 # date: 2018/1/9 3 4 from bs4 import BeautifulSoup # 導入BeautifulSoup解析庫 5 import requests # 請求網站獲取網頁數據 6 import json # python中的一種輕量級的數據交換格式 7 8 # import sys # python中的操作系統模塊 9 # sys.getdefaultencoding() 10 11 # 網站代碼解析 12 def parser(html, items): 13 # soup是一個BeautifulSoup對象,把HTML代碼轉換成樹形結構存儲在soup中 14 soup = BeautifulSoup(html, 'html.parser') 15 # 使用BeautifulSoup中的select方法提取HTML代碼中的信息 16 even = soup.select('.even') 17 odd = soup.select('.odd') 18 str = odd + even # 字符串拼接 19 for item in str: # for循環迭代(遍歷) 20 # _item為一個字典,存儲一個工作職位的信息 21 _item = {} 22 # 職位名稱 23 _item['name_of_work'] = item.select('td')[0].get_text() 24 # 職位鏈接 25 _item['link_of_work'] = 'http://hr.tencent.com' + item.select('td a')[0].attrs['href'] 26 # 職位類別 27 _item['category_of_work'] = item.select('td')[1].get_text() 28 # 工作地點 29 _item['where_of_work'] = item.select('td')[3].get_text() 30 # 發布時間 31 _item['time_of_release'] = item.select('td')[4].get_text() 32 # 所需人數 33 _item['number_of_person'] = item.select('td')[2].get_text() 34 # 把以上信息添加到列表中 35 items.append(_item) 36 37 # 請求獲取網站頁面的信息(html代碼) 網址:http://hr.tencent.com/position.php 38 def load_request(number, items): 39 # User-Agent: 請求報頭,用來把程序偽裝成瀏覽器 40 headers = {"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36"} 41 # 使用requests的get請求服務器獲取信息 42 request = requests.get('http://hr.tencent.com/position.php?&start='+str(number)+"#a", headers=headers) 43 # 取出返回信息的文本信息(html代碼) 44 html = request.text 45 parser(html, items) 46 47 if __name__ == "__main__": 48 number = 0 # 49 items = [] # 列表 50 swith = True 51 while swith: 52 if number >= 50: 53 # swith相當於一個開關請示,如果請求發出,爬蟲啟動 54 swith = False 55 load_request(number, items) 56 # 一次性爬取10頁數據 57 number += 10 58 content = json.dumps(items, ensure_ascii=False) 59 # 數據讀入json中 60 with open('txzhaoping.json', 'w',encoding='utf-8') as f: 61 f.write(content)
運行程序,結果如下: