I want to extract the name enter image description here of those who like the post with open the every profiles.
I try this method but now getting all the name.
How I can extract the LinkedIn post likes members name and designation using selenium with opening profile?
will be used in the while loop
while True:
time.sleep(5)
show_more=show_likes.find_element_by_class_name("display-flex p5")
print("show more output---\>",show_more)
show_more.click()
print(show_more)
end = time.time()
if round(end - start) \> 60:
break
CodePudding user response:
The best way to scrape data from a dynamic list is to delete each element from the HTML once data is scraped. Usually when there are few elements left, new ones are automatically loaded (as if you are scrolling down), but sometimes there is a "show more" button to click in order to load new elements.
The following code:
- Gets the list of people
- if list is empty (no new elements were loaded) terminates the execution
- if there is a show more button, click it
- loop over the list of people, prints name and job, remove the person from HTML
.
while 1:
people = driver.find_elements(By.XPATH, "//ul[contains(@class,'artdeco-list')]/li")
if not people:
print('list "people" is empty')
break
show_more_btn = driver.find_elements(By.XPATH, '//div[@id="artdeco-modal-outlet"]//button[contains(@class,"load-button")]')
if show_more_btn:
show_more_btn[0].click()
for person in people:
name = person.find_element(By.XPATH, './/div[contains(@class,"artdeco-entity-lockup__title")]').text.split('\n')[0]
job = person.find_element(By.XPATH, './/div[contains(@class,"artdeco-entity-lockup__caption")]').text
print(name, '-' , job)
time.sleep(0.5)
driver.execute_script('var element = arguments[0]; element.remove();', person)