[英]LinkedIn profile info scraping using Selenium
我正在嘗試從 LinkedIn 抓取個人資料,我從下面的代碼中獲取個人資料 URL,並希望將其傳遞給 driver.get(URL),但是當我抓取 URL 時,URL 的格式是不同的,例如它在 [] 括號中和我收到這個錯誤
selenium.common.exceptions.InvalidArgumentException:消息:無效參數:“url”必須是字符串
您能否建議如何在列表linklist = [ ]中獲取正確格式的 URL,以便我可以將它們傳遞給driver.get(URL) 。 謝謝!
options = Options()
options.add_argument("--start-maximized")
options.headless = True
url = "https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin"
driver = webdriver.Chrome(path, options=options)
driver.get(url)
driver.find_element_by_id('username').send_keys('name')
driver.find_element_by_id('password').send_keys('password', Keys.ENTER)
driver.implicitly_wait(10)
driver.find_element_by_class_name('search-global-typeahead__input').send_keys('Marketing manager', Keys.ENTER)
driver.implicitly_wait(10)
driver.find_element_by_xpath('//button[text()="People"]').click()
x = 0
profile = []
linklist = []
condition = True
while condition:
sleep(2)
driver.execute_script("window.scrollTo(0, 1400);")
driver.implicitly_wait(10)
linkedin_members = driver.find_elements_by_xpath('//span[@class="entity-result__title"]')
links = [linkedin_member.find_element_by_xpath('.//a[@class="app-aware-link"]').get_attribute('href') for linkedin_member in linkedin_members if "/in/" in linkedin_member.find_element_by_xpath('.//a[@class="app-aware-link"]').get_attribute('href')]
x = x + 1
linklist.append(link for link in links)
driver.implicitly_wait(10)
driver.find_element_by_xpath("""//button[@class='artdeco-pagination__button artdeco-pagination__button--next artdeco-button artdeco-button--muted artdeco-button--icon-right artdeco-button--1 artdeco-button--tertiary ember-view' and contains(.,'Next')]""").click()
if x == 2:
condition = False
profile = []
for l in tqdm(linklist):
driver.get(l)
我使用for循環代替您使用的while循環,因為沒有可變條件,您只想執行兩次循環。
以下是您的操作方法:
linklist = []
for i in range(2):
time.sleep(2)
driver.execute_script("window.scrollTo(0, 1400);")
driver.implicitly_wait(10)
linkedin_members = driver.find_elements_by_xpath('//span[@class="entity-result__title"]')
link = driver.find_element_by_class_name('app-aware-link').get_attribute('href')
linklist.append(link)
driver.implicitly_wait(10)
driver.find_element_by_xpath("""//button[@class='artdeco-pagination__button artdeco-pagination__button--next artdeco-button artdeco-button--muted artdeco-button--icon-right artdeco-button--1 artdeco-button--tertiary ember-view' and contains(.,'Next')]""").click()
for url in linklist:
driver.get(url)
我搜索了包含配置文件 url 的 class 並使用“ .get_attribute('href') ”來提取 url。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.