對于我所做的某些作業,我需要收集有關職位名稱以及它們在搜索結果中出現的頻率的資料,因此我決定使用 Python 來幫助我解決這個問題。唯一的問題是我似乎無法弄清楚為什么我發現的這個代碼片段沒有給我我需要的正確資訊。這是我到目前為止所擁有的:
import requests
from bs4 import BeautifulSoup
from collections import Counter
from string import punctuation
# We get the url
r = requests.get("https://www.usajobs.gov/Search/Results?j=0602&d=VA&p=1")
soup = BeautifulSoup(r.content, "html.parser")
# We get the words within divs
text_div = (''.join(s.findAll(text=True))for s in soup.findAll('div'))
c_div = Counter((x.rstrip(punctuation).lower() for y in text_div for x in y.split()))
total = c_div
print(total)
我知道這其中的一部分涉及檢查代碼,但我無法弄清楚我需要輸入什么才能讓刮板縮小到這些標題:
<a id="usajobs-search-result-0" href="/GetJob/ViewDetails/568337700" itemprop="title" data-document-id="568337700">

將不勝感激任何幫助
uj5u.com熱心網友回復:
資料通過發送POST請求動態加載到:
https://www.usajobs.gov/Search/ExecuteSearch
請參閱此示例以獲取正確的職位。(您可以更改page鍵以指定頁碼)。
import requests
data = {
"JobTitle": [],
"GradeBucket": [],
"JobCategoryCode": ["0602"],
"JobCategoryFamily": [],
"LocationName": [],
"PostingChannel": [],
"Department": ["VA"],
"Agency": [],
"PositionOfferingTypeCode": [],
"TravelPercentage": [],
"PositionScheduleTypeCode": [],
"SecurityClearanceRequired": [],
"PositionSensitivity": [],
"ShowAllFilters": [],
"HiringPath": [],
"SocTitle": [],
"MCOTags": [],
"CyberWorkRole": [],
"CyberWorkGrouping": [],
"Page": "1", # <-- Change page number here
"UniqueSearchID": "9d417c5e-adc2-469c-af1d-e786cc41bc97",
"IsAuthenticated": "false",
}
response = requests.post(
"https://www.usajobs.gov/Search/ExecuteSearch", json=data
).json()
job_titles = [job["Title"] for job in response["Jobs"]]
print(job_titles)
輸出:
['Psychiatrist - OCA', 'Physician - Electromyography (Temporary)', 'Physician Owensboro CBOC PC', 'Physician-Primary Care', 'OPHTHALMOLOGIST', 'UROLOGIST', 'PHYSICIAN (OTOLARYNGOLOGIST', 'Physician-Hospitalist', 'Physician - Hemotology/Oncology', 'Academic Gastroenterologist', 'Physician - Gastroenterologist', 'Physician - Orthopedic Surgeon', 'Physician (Internal Medicine or Family Practice)', 'Physician (Regular Ft)- Hematologist/Oncologist', 'Physician- Hematologist/Oncologist', 'Physician - Diagnostic Radiologist', 'Physician (Psychiatrist)', 'Physician (Endocrinologist)', 'Physician (Cardiologist)', 'Physician (Neurologist)', 'Physician (Chief Hospitalist)', 'Physician (Hospitalist)', 'Physician (Medical Director of Extended Care/Chief of Geriatrics)', 'Physician (Primary Care)', 'Physician (Hematologist/Oncologist)']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/324853.html
