This repository has been archived by the owner on Jan 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch catalog and faculty scraper to aiohttp
- Loading branch information
1 parent
a35afb6
commit 82138c0
Showing
3 changed files
with
195 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
from concurrent import futures | ||
import asyncio | ||
import time | ||
import requests | ||
import aiohttp | ||
from bs4 import BeautifulSoup | ||
import json | ||
from tqdm import tqdm | ||
|
||
|
||
# response = requests.get(url='https://faculty.rpi.edu/data/peoplesearch') | ||
# soup = BeautifulSoup(response.text.encode('utf8'), "html") | ||
async def get_professor(s, professor, url, data): | ||
async with s.get(url) as response: | ||
soup = BeautifulSoup(await response.text("utf8"), "lxml") | ||
data[professor["node"]["Path"]] = {} | ||
professor_data = data[professor["node"]["Path"]] | ||
professor_data["name"] = professor["node"]["title"].strip() | ||
|
||
faculty = requests.get('https://faculty.rpi.edu/data/peoplesearch').json() | ||
# Find all fields on the professor page and append them to the data object | ||
for item in soup.findAll("div", {"class": "views-field"}): | ||
class_parts = item["class"][1].split("-") | ||
if class_parts[len(class_parts) - 1] == "portrait": | ||
professor_data[class_parts[len(class_parts) - 1]] = item.find("img")[ | ||
"src" | ||
].split("?")[0] | ||
elif item.find("div", {"class": "field-content"}): | ||
professor_data[class_parts[len(class_parts) - 1]] = " ".join( | ||
item.find("div", {"class": "field-content"}).get_text(" ").split() | ||
) | ||
|
||
data = {} | ||
for professor in tqdm(faculty['nodes']): | ||
response = requests.get(url=f"https://faculty.rpi.edu{professor['node']['Path']}") | ||
soup = BeautifulSoup(response.text.encode('utf8'), "lxml") | ||
data[professor['node']['title'].strip()] = {} | ||
professor_data = data[professor['node']['title'].strip()] | ||
professor_data['url'] = professor['node']['Path'] | ||
for item in soup.findAll("div", {"class": "views-field"}): | ||
class_parts = item['class'][1].split('-') | ||
if class_parts[len(class_parts)-1] == 'portrait': | ||
professor_data[class_parts[len(class_parts)-1]] = item.find("img")['src'].split('?')[0] | ||
elif item.find("div", {"class": "field-content"}): | ||
professor_data[class_parts[len(class_parts)-1]] = " ".join(item.find("div", {"class": "field-content"}).get_text(" ").split()) | ||
|
||
async def main(): | ||
# Gets a json object that contains all faculty and their associated urls | ||
faculty = requests.get("https://faculty.rpi.edu/data/peoplesearch").json() | ||
|
||
with open(f"faculty.json", "w") as outfile: | ||
json.dump(data , outfile, sort_keys=False, indent=2) | ||
data = {} | ||
async with aiohttp.ClientSession() as s: | ||
await asyncio.gather( | ||
*( | ||
get_professor( | ||
s, | ||
professor, | ||
f"https://faculty.rpi.edu{professor['node']['Path']}", | ||
data, | ||
) | ||
for professor in faculty["nodes"] | ||
) | ||
) | ||
|
||
with open("faculty.json", "w") as outfile: | ||
json.dump(data, outfile, sort_keys=True, indent=2) | ||
|
||
|
||
asyncio.run(main()) |
Oops, something went wrong.