??大家好,我是不溫卜火,是一名計算機學院大資料專業大三的學生,昵稱來源于成語—
不溫不火,本意是希望自己性情溫和,作為一名互聯網行業的小白,博主寫博客一方面是為了記錄自己的學習程序,另一方面是總結自己所犯的錯誤希望能夠幫助到很多和自己一樣處于起步階段的萌新,但由于水平有限,博客中難免會有一些錯誤出現,有紕漏之處懇請各位大佬不吝賜教!暫時只在csdn這一個平臺進行更新,博客主頁:https://buwenbuhuo.blog.csdn.net/,
PS:由于現在越來越多的人未經本人同意直接爬取博主本人文章,博主在此特別宣告:未經本人允許,禁止轉載!!!
目錄
- 一、網頁分析
- 二、功能實作
- 2.1 拼接URL
- 2.2 獲取資料
- 三、完整代碼
- 四、保存結果

剛剛經過了豆瓣電影的爬取,你是不是有點懵逼呢?那么博主今天帶來一篇較為簡單得動態html資料采集的文章,
今天我們來爬取騰訊招聘的相關資訊,
鏈接:https://careers.tencent.com/search.html

一、網頁分析
首先我們打開鏈接,如下圖:

通過查看原始碼,我們發現其并不是靜態網頁,因此可以初步判定其為動態網頁

這樣我們的方向就明朗起來了,我們只需找到API介面就可以獲取資料,打開開發者選項,通過查找找到我們的API介面


到這里分析已經完成了,那么接下來先嘗試獲取整個介面資訊,因為我們只有先獲取資料才有可能繼續下一步操作,
# encoding: utf-8
'''
@author 李華鑫
@create 2020-10-19 12:28
Mycsdn:https://buwenbuhuo.blog.csdn.net/
@contact: 459804692@qq.com
@software: Pycharm
@file: test.py
@Version:1.0
'''
import requests
url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079775850&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=1&pageSize=10&language=zh-cn&area=cn"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}
def parse_json(url, params={}):
"""決議url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()
content = parse_json(url)
print(content)


🆗,看來我們是成功獲取到資料了,
下面我們來分析下,每一頁URL之間的關系,
首先,我們看下其中幾個URL
https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079775850&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=1&pageSize=10&language=zh-cn&area=cn
https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079981956&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn
https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1603079981956&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=&pageIndex=3&pageSize=10&language=zh-cn&area=cn

通過對比,我們發現只有pageIndex有變化,那么我們現在可以驗證下猜想


通過對比,驗證了我們的猜想,下面我們就只需要看總共有多少頁即可

分析了頁數,那么如果想要回圈爬取全部網頁,只需進行拼接即可
for i in range(1,635):
params["pageIndex"] = i
二、功能實作

2.1 拼接URL
通過上述的分析,我們知道只需修改每回的pageIndex即可,
首先我們先把需要拼接的部分復制出來

讓其轉換成字典的方式
xx="""timestamp: 1603081773061
countryId:
cityId:
bgIds:
productId:
categoryId:
parentCategoryId:
attrId:
keyword:
pageIndex: 2
pageSize: 10
language: zh-cn
area: cn"""
xx = xx.splitlines()
params = {}
for x in xx:
print(x.split(":"))
params[x.split(":")[0]] = x.split(":")[1]
from pprint import pprint
pprint(params)

下面就開始代碼實作此部分
params = {'area': ' cn',
'attrId': ' ',
'bgIds': ' ',
'categoryId': ' ',
'cityId': ' ',
'countryId': ' ',
'keyword': ' ',
'language': ' zh-cn',
'pageIndex': ' 1',
'pageSize': ' 10',
'parentCategoryId': ' ',
'productId': ' ',
'timestamp': ' 1602211262824'}
def parse_json(url, params={}):
"""決議url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()
def start():
for i in range(1,635):
params["pageIndex"] = i
if __name__ == '__main__':
start()
2.2 獲取資料

由于之前已經講解過此部分,因此此處只給出代碼
def get_position(data):
"""獲取職位資料"""
item = {
"postion_name":"",#職位名稱
"postion_department":"",#職位部門
"postion_location":"",#職位所在地
"postion_country":"",#職位所在國家
"postion_category":"",#職位類別
"postion_responsibility":"",#職位職責
"postion_url":"",#職位url
}
data_list = data["Data"]["Posts"]
for data in data_list:
item["postion_name"] = data["RecruitPostName"]
item["postion_department"] = data["BGName"]
item["postion_location"] = data["LocationName"]
item["postion_country"] = data["CountryName"]
item["postion_category"] = data["CategoryName"]
item["postion_responsibility"] = data["Responsibility"]
item["postion_url"] = data["PostURL"]
三、完整代碼

# encoding: utf-8
'''
@author 李華鑫
@create 2020-10-09 9:38
Mycsdn:https://buwenbuhuo.blog.csdn.net/
@contact: 459804692@qq.com
@software: Pycharm
@file: 騰訊招聘.py
@Version:1.0
'''
import requests
import csv
url = "https://careers.tencent.com/tencentcareer/api/post/Query"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}
params = {'area': ' cn',
'attrId': ' ',
'bgIds': ' ',
'categoryId': ' ',
'cityId': ' ',
'countryId': ' ',
'keyword': ' ',
'language': ' zh-cn',
'pageIndex': ' 1',
'pageSize': ' 10',
'parentCategoryId': ' ',
'productId': ' ',
'timestamp': ' 1602211262824'}
def parse_json(url, params={}):
"""決議url,得到字典"""
response = requests.get(url=url, headers=headers, params=params)
return response.json()
def get_position(data):
"""獲取職位資料"""
item = {
"postion_name":"",#職位名稱
"postion_department":"",#職位部門
"postion_location":"",#職位所在地
"postion_country":"",#職位所在國家
"postion_category":"",#職位類別
"postion_responsibility":"",#職位職責
"postion_url":"",#職位url
}
data_list = data["Data"]["Posts"]
for data in data_list:
item["postion_name"] = data["RecruitPostName"]
item["postion_department"] = data["BGName"]
item["postion_location"] = data["LocationName"]
item["postion_country"] = data["CountryName"]
item["postion_category"] = data["CategoryName"]
item["postion_responsibility"] = data["Responsibility"]
item["postion_url"] = data["PostURL"]
save(item)
print(item)
print("保存完成")
def save(item):
"""將資料保存到csv中"""
with open("./騰訊招聘.csv", "a", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(item.values())
def start():
for i in range(1,635):
params["pageIndex"] = i
data = parse_json(url,params)
get_position(data)
if __name__ == '__main__':
start()
四、保存結果


美好的日子總是短暫的,雖然還想繼續與大家暢談,但是本篇博文到此已經結束了,如果還嫌不夠過癮,不用擔心,我們下篇見!

??好書不厭讀百回,熟讀課思子自知,而我想要成為全場最靚的仔,就必須堅持通過學習來獲取更多知識,用知識改變命運,用博客見證成長,用行動證明我在努力,
??如果我的博客對你有幫助、如果你喜歡我的博客內容,請“點贊” “評論”“收藏”一鍵三連哦!聽說點贊的人運氣不會太差,每一天都會元氣滿滿呦!如果實在要白嫖的話,那祝你開心每一天,歡迎常來我博客看看,
??碼字不易,大家的支持就是我堅持下去的動力,點贊后不要忘了關注我哦!


轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/195778.html
標籤:其他

