-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.py
49 lines (34 loc) · 1.29 KB
/
crawler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import os
import requests
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
load_dotenv()
service_path = os.getenv("CHROME_DRIVER_PATH")
BASE_URL = os.getenv("BASE_URL")
service = Service(service_path)
driver = webdriver.Chrome(service=service)
PLAYERS_API_ENDPOINT = f"{BASE_URL}/483e026bce6c25d2041d"
UPDATE_ENDPOINT = f"{BASE_URL}/ab0d985e937ec02b5021"
resp = requests.get(PLAYERS_API_ENDPOINT, headers={"Content-Type": "application/json"}).json()
updated_players = []
for player in resp["players"]:
query = player["name"]
url = "https://www.google.com/search?q="+query+"&source=lnms&tbm=isch"
driver.get(url=url)
image = driver.find_element(by=By.CSS_SELECTOR, value=".rg_i.Q4LuWd")
image_url = image.get_attribute("src")
updated_players.append(
{
"id": player['id'],
"name": player['name'],
"image": image_url
}
)
print(f"Gotten image url for Player {player['id']}...")
updated_players_json = json.dumps({"players": updated_players})
update_response = requests.post(url=UPDATE_ENDPOINT, data=updated_players_json)
if update_response.status_code == 200:
print("\nSuccess running script.")