I am a beginner in python,im just trying to scrape web with module requests
and BeautifulSoup
This Website i make request.
and my simple code:
import requests, time, re, json
from bs4 import BeautifulSoup as BS
url = "https://www.jobstreet.co.id/en/job-search/job-vacancy.php?ojs=6"
def list_jobs():
try:
with requests.session() as s:
st = time.time()
s.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0'}
req = s.get(url)
soup = BS(req.text,'html.parser')
attr = soup.findAll('div',class_='position-title header-text')
pttr = r".?(.*)Rank=\d+"
lists = {"status":200,"result":[]}
for a in attr:
sr = re.search(pttr, a.find("a")["href"])
if sr:
title = a.find('a')['title'].replace("Lihat detil lowongan -","").replace("\r","").replace("\n","")
url = a.find('a')['href']
lists["result"].append({
"title":title,
"url":url,
"detail":detail_jobs(url)
})
print(json.dumps(lists, indent=4))
end = time.time() - st
print(f"\n{end} second")
except:
pass
def detail_jobs(find_url):
try:
with requests.session() as s:
s.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0'}
req = s.get(find_url)
soup = BS(req.text,'html.parser')
position = soup.find('h1',class_='job-position').text
name = soup.find('div',class_='company_name').text.strip("\t")
try:
addrs = soup.find('div',class_='map-col-wraper').find('p',{'id':'address'}).text
except Exception:
addrs = "Unknown"
try:
loct = soup.find('span',{'id':'single_work_location'}).text
except Exception:
loct = soup.find('span',{'id':'multiple_work_location_list'}).find('span',{'class':'show'}).text
dests = soup.findAll('div',attrs={'id':'job_description'})
for select in dests:
txt = select.text if not select.text.startswith("\n") or not select.text.endswith("\n") else select.text.replace("\n","")
result = {
"name":name,
"location":loct,
"position":position,
"description":txt,
"address":addrs
}
return result
except:
pass
they all work well but take very long to show results time is always above 13/17 seconds
i dont know how to increase my speed for requesting
I tried search on stack and google,they said using asyncio but the way so hard to me.
if someone have simple trick how to increase speed with simple do,im so appreciate ..
And sorry for my bad English