今天我們要爬取的是拉勾網的招聘信息
需求1:
- 獲取以下信息
- 城市
- 公司名
- 公司規模
- 學歷
- 職位名稱
- 薪資
- 工作時間
需求2:
以逗號(,)分割信息內容,寫入csv文件。
網址分析:
- URL :https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=
- 查看網址源碼
這麼多數據包,到底哪一個是我們需要的呢?
搜索一下關鍵詞
確定裡面包含了我們需要的信息,那麼我們開始進行編寫爬蟲
取出headers裡面的url
<code>import requests
import json
api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
}
response = requests.post(api_url)
result = response.json()
print(result)/<code>
我們嘗試一下打印:
得到以下結果,提示操作太頻繁,但是隻請求了一次呀,請求頭也加了,怎麼會這樣呢?
把以下幾個參數加入試試;
<code>import requests
import json
url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
}
response = requests.post(api_url,headers=header)
result = response.json()
print(result)/<code>
打印以下結果看看
還是提示操作太頻繁了。user-agent和cookie都傳了,為什麼還是不行呢?
分析一下:
- 首先,我們的ip肯定是沒有被封的。
- 根據http協議原理,cookie是http客戶端服務器設置的
- js可以修改cookie
- 那麼我們來清理一下所有的cookie
再次刷新網頁
服務器重新給我們返回了cookie,裡面包含著一些session,id等等信息
那麼我們嘗試是一下那個是我們需要的:
再次打印:
成功返回我們需要的數據~
但是發現一個問題,這個cookie是不穩定的,使用的次數多了,還是會出現操作太頻繁的警告,那麼怎麼實現一勞永逸呢?
向https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=這個url發起請求。
<code>url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
responses = requests.get(url,headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
})
cookie = responses.cookies
print(cookie)/<code>
打印一下:
出現了我們需要的cookie,成功解決了我們剛才的不穩定因素。
下面我們把cookie加入到請求裡面
<code>url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
responses = requests.get(url,headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
})
cookie = responses.cookies/<code>
下面開始對數據進行處理
通過返回的數據可以分析出,我們需要的數據在conment——positionResult——result這樣的層級裡面,下面我們開始編寫代碼。
<code>import requests
import json
url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
responses = requests.get(url,headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
})
cookie = responses.cookies
api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
\t\t'Host':'www.lagou.com'
\t\t'Origin':'https://www.lagou.com'
\t\t'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}
data = {
'first': 'true',
'pn':'1',
'kd':'python',
}
response = requests.post(api_url,headers=header,data=data,cookies=cookie) #發起請求
result = response.json() #轉換成json格式的
results = result['content']['positionResult']['result'] #通過層級獲取result裡面的信息
for i in results:# 遍歷數據
d = { #以字典的形式展示
'city':i['city'],
'companyFullName':i['companyFullName'],
'companySize':i['companySize'],
'education':i['education'],
'positionName':i['positionName'],
'salary':i['salary'],
'workYear':i['workYear'],
}
\t\tprint('d')/<code>
打印結果:
好啦,第一個需求已經完成,下面開始完成第二個需求;
以逗號(,)分割信息內容,寫入csv文件。
話不多說,開始編寫代碼
<code>import requests
import json
url='https://www.lagou.com/jobs/list_python/p-city_0?&cl=false&fromSearch=true&labelWords=&suginput='
responses = requests.get(url,headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
})
cookie = responses.cookies
api_url ='https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
\t\t'Host':'www.lagou.com'
\t\t'Origin':'https://www.lagou.com'
\t\t'Referer':'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
}
data = {
'first': 'true',
'pn':'1',
'kd':'python',
}
response = requests.post(api_url,headers=header,data=data,cookies=cookie) #發起請求
result = response.json() #轉換成json格式的
results = result['content']['positionResult']['result'] #通過層級獲取result裡面的信息
for i in results:# 遍歷數據
d = { #以字典的形式展示
'city':i['city'],
'companyFullName':i['companyFullName'],
'companySize':i['companySize'],
'education':i['education'],
'positionName':i['positionName'],
'salary':i['salary'],
'workYear':i['workYear'],
}
\t\twith open('拉鉤職位信息.csv','a',encoding='utf-8')as f:
\t\tf.write(','.join(d.values()))
f.write('\\n')/<code>
運行一下
出現一個後綴為.csv的文件,我們打開看看吧。
大功告成了。
總結
- 本章我們學習瞭如何實現應對反爬。
- 通過數據分析得知,一個數據包只有15條數據(參考數據處理那一段的圖片)那麼我們想要30,100條甚至更多呢?
- 注意:如果批量爬取,一定要設置延時,否則爬太快容易封ip,會導致一段時間無法進入該網站,同時也是很不友好的行為。
- 本文僅供學習交流,請勿商用。謝謝
閱讀更多 Python奇異果 的文章