recent

6/recent/ticker-posts

Header Ads Widget

5 ChatGPT features to boost your daily work

 

And how to enhance your code quality using it

Self-made image.

#1. Generate your coding skeleton

Screenshot ChatGPT chat. ChatGPT gives me a code skeleton.
Screenshot ChatGPT chat. ChatGPT gives me a code skeleton.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the web driver
driver = webdriver.Firefox()

# Navigate to LinkedIn
driver.get("https://www.linkedin.com/")

# Find the email and password input fields
email = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

# Enter your credentials
email.send_keys("YOUR_EMAIL")
password.send_keys("YOUR_PASSWORD")

# Click the login button
driver.find_element_by_xpath("//button[@type='submit']").click()

# Wait for the page to load
time.sleep(5)

# Navigate to the profile of the desired user
driver.get("https://www.linkedin.com/in/USER_PROFILE_NAME")

# Extract the information you want from the page
name = driver.find_element_by_xpath("//span[@class='inline t-24 t-black t-normal break-words']").text
location = driver.find_element_by_xpath("//span[@class='t-16 t-black t-normal inline-block']").text

print(name)
print(location)

# close the browser
driver.quit()

#2. Research and compare

Screenshot ChatGPT chat. ChatGPT explains to me the differences between geopandas and plotly.
Screenshot ChatGPT chat. ChatGPT explains to me the differences between geopandas and plotly.
Screenshot ChatGPT chat. ChatGPT explains the most popular scraping website
Screenshot ChatGPT chat. ChatGPT explains the best option to scrape a social network.
Screenshot ChatGPT chat. ChatGPT explains the best option to scrape a social network.

#3. Understanding code

#We find how many jobs are offered.
jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText")
if len(jobs_num.split(',')) > 1:
jobs_num = int(jobs_num.split(',')[0])*1000
else:
jobs_num = int(jobs_num)

jobs_num = int(jobs_num)

#Here I choose manually a number of jobs, so it wont take that long:
jobs_num = 1000;


#We create a while loop to browse all jobs.
i = 2
while i <= int(jobs_num/2)+1:
#We keep scrollind down to the end of the view.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
i = i + 1
print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r")
try:
#We try to click on the load more results buttons in case it is already displayed.
infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']")
infinite_scroller_button.click()
time.sleep(0.1)
except:
#If there is no button, there will be an error, so we keep scrolling down.
time.sleep(0.1)
pass
Screenshot ChatGPT chat. ChatGPT explains the code I didn’t understand.
Screenshot ChatGPT chat. ChatGPT explains the code I didn’t understand.

#4. Adding comments to our code

# Find the number of jobs offered on the website
jobs_num = driver.find_element(By.CSS_SELECTOR,"h1>span").get_attribute("innerText")

# Check if the number of jobs contains a ',' and if so, convert it to number * 1000
if len(jobs_num.split(',')) > 1:
jobs_num = int(jobs_num.split(',')[0])*1000
else:
jobs_num = int(jobs_num)

# Convert jobs_num to an integer
jobs_num = int(jobs_num)

# Set the number of jobs to scrape to 1000
jobs_num = 1000;

# Initialize a counter variable to scroll through the jobs
i = 2

# Start a while loop to scroll through the jobs
while i <= int(jobs_num/2)+1:
# Scroll to the bottom of the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Increment the counter variable
i = i + 1
# Print the current number of jobs scrolled through and the percentage of completion
print("Current at: ", i, "Percentage at: ", ((i+1)/(int(jobs_num/2)+1))*100, "%",end="\r")
try:
# Try to locate the "Load more results" button and click on it
infinite_scroller_button = driver.find_element(By.XPATH, ".//button[@aria-label='Load more results']")
infinite_scroller_button.click()
# Sleep for 0.1 seconds
time.sleep(0.1)
except:
# If the button is not present, sleep for 0.1 seconds and continue scrolling
time.sleep(0.1)
pass

#5. Rewriting our code using some style

Screenshot ChatGPT chat. ChatGPT giving our code following Pep8 standard.
Screenshot ChatGPT chat. ChatGPT giving our code following Pep8 standard.

Main Conclusions

Post a Comment

0 Comments