问:Generate boiler plate code that integrates an extern API using Python.
或者我们甚至可以要求这个框架使用 Selenium 抓取 LinkedIn
ChatGPT 的输出如下:
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
问:What does the following code do? [insert code here]
#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
问:Add comments to the following code [insert code here]
# 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
评论