简体   繁体   English

如何使用 selenium 和 python 获取谷歌搜索结果链接

[英]How to fetch google search results links using selenium and python

I am trying to fetch all the search results link and print it.我正在尝试获取所有搜索结果链接并打印出来。 The problem I found is I am not able to iterate using the xpath of the individual search results because我发现的问题是我无法使用单个搜索结果的 xpath 进行迭代,因为

elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a') elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a')

is one result and the next result's link is是一个结果,下一个结果的链接是

elems = driver.find_elements_by_xpath('div[3]/div/div[1]/a') elems = driver.find_elements_by_xpath('div[3]/div/div[1]/a')

The index value of div is what iterates throughout the results. div 的索引值是在整个结果中迭代的值。 Currently I am able to fetch only one link with the index value of div.目前我只能用 div 的索引值获取一个链接。 I want to fetch all of the results and append it to a list for further processing.我想获取所有结果并将 append 提取到列表中以进行进一步处理。 Help me out on this one帮我解决这个问题

driver = webdriver.Chrome()
driver.get('http://www.google.com')

search_input = driver.find_element_by_name('q')
search_input.send_keys('stackoverflow')
search_input.send_keys(Keys.ENTER)
results = []
elems = driver.find_elements_by_xpath('div[2]/div/div[1]/a')
for elem in elems:
    href = elem.get_attribute('href')
    print(href)
    results.append(href)

Create a counter i and place it in your xpath.创建一个计数器 i 并将其放在您的 xpath 中。 Suppose i is integer counter before using it in xpath convert it into string using假设我是 integer 计数器,然后在 xpath 中使用将其转换为字符串

  string= str(i)
  elems = driver.find_elements_by_xpath("div['"+ string +"']/div/div[1]/a")
    import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class GooglesearchTests {

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver83\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
        driver.findElement(By.name("q")).sendKeys("Testing");
        Thread.sleep(Long.parseLong("1000"));
        List<WebElement> LIST = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
        System.out.println(LIST.size());


        for (int i = 0; i < LIST.size(); i++)
        {
            //System.out.println(LIST.get(i).getText());
            if (LIST.get(i).getText().contains("testing types"))
            {
                LIST.get(i).click();
                break;
            }
        }
    }
}


This is java code for google search results 


every day google page elements are changing ( What i am doing is open google and type testing and store all results in list and pick "Testing Type" from list

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

相关问题 在 selenium python 中每次搜索获取 google 链接搜索结果 - get google links search results per search in selenium python 如何使用Selenium,Python从Google搜索中提取链接 - How to Pull Links from Google Search using Selenium, Python 使用 python selenium 迭代谷歌搜索结果 - Iterating google search results using python selenium 在Python中使用硒打印Google搜索结果 - Print google search results using selenium in Python 如何从 Google 搜索结果中抓取所有标题和链接(Python + Selenium) - How to scrape all the titles and links from Google search results (Python + Selenium) 使用 Selenium Python 获取所有链接 - Fetch all links using Selenium Python 如何使用 selenium 滚动谷歌地图搜索显示的结果 - How to scroll the results shown by google maps search using selenium 使用硒使用jscript _doPostBack链接显示“下一个”搜索结果 - using selenium to display 'next' search results using jscript _doPostBack links 如何使用 Selenium Python 从搜索结果中收集关键字 - How to collect the keywords from the search results using Selenium Python 如何使用 Selenium 和 Python 在 google 上点击第一个 google 搜索结果 - How to click on the first google search result on google using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM