简体   繁体   English

在 selenium python 中每次搜索获取 google 链接搜索结果

[英]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亲爱的,我正在尝试获取谷歌搜索结果的链接,我正在使用这个配置文件 = driver.find_elements_by_xpath('//*[@class="r"]/a[1]')这工作正常,我是现在使用它,但没有给我结果

            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]')什么是 xpath 可以用来代替这个 driver.find_elements_by_xpath('//*[@class="r"]/a[1]')

based on MBaas answer i tried this.基于 MBaas 的回答,我尝试了这个。 I added it so that will first find the search result, then find based on css selector ("div > div > div.yuRUbf > a").我添加它是为了首先找到搜索结果,然后根据 css 选择器(“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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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