簡體   English   中英

在 selenium python 中每次搜索獲取 google 鏈接搜索結果

[英]get google links search results per search in selenium python

親愛的,我正在嘗試獲取谷歌搜索結果的鏈接,我正在使用這個配置文件 = 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()

什么是 xpath 可以用來代替這個 driver.find_elements_by_xpath('//*[@class="r"]/a[1]')

基於 MBaas 的回答,我嘗試了這個。 我添加它是為了首先找到搜索結果,然后根據 css 選擇器(“div > div > div.yuRUbf > a”)進行查找。

希望這可以幫助, 在此處輸入圖像描述

# 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