简体   繁体   中英

get google links search results per search in selenium python

Dears, i am trying to get the links of google search results, i was using this profiles = driver.find_elements_by_xpath('//*[@class="r"]/a[1]') this was working correctly, i am using it now, but not giving me results

            import csv
            from parsel import Selector
            from selenium import webdriver
            from selenium.webdriver.common.keys import Keys
            import time
            import itertools
            import random
            listlong = []
            
            driver = webdriver.Chrome('C:/chromedriver.exe')
            driver.get('https://www.google.com/search?q=&num=200&start=0&sourceid=chrome')
            time.sleep(random.randint(10,11))
            search_input = driver.find_element_by_name('q')
            search_input.send_keys('site:linkedin.com/in/ AND USA')
            time.sleep(3)
            search_input.send_keys(Keys.RETURN)
            time.sleep(3)
            # grab all linkedin profiles from first page at Google
            profiles = driver.find_elements_by_xpath('//*[@class="r"]/a[1]')
            time.sleep(8)
            profiles = [profile.get_attribute('href') for profile in profiles]
            listprof = profiles.copy()
            listlong.extend(listprof)
            print(profiles)
            print("f1")
            print(listprof)
            print("f2")
            print(listlong)
            print("f3")
            driver.quit()

what is the xpath can be used instead of this driver.find_elements_by_xpath('//*[@class="r"]/a[1]')

based on MBaas answer i tried this. I added it so that will first find the search result, then find based on css selector ("div > div > div.yuRUbf > a").

Hope this helps, 在此处输入图像描述

# grab all linkedin profiles from first page at Google
search_result = driver.find_elements_by_css_selector('div.g')
profiles = driver.find_elements_by_css_selector("div > div > div.yuRUbf > a")
time.sleep(8)
profiles = [profile.get_attribute('href') for profile in profiles]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM