我如何提出“人们也要问”文字。使用Selenium和Python的Google搜索部分

发布于 2025-02-08 16:15:10 字数 802 浏览 5 评论 0原文

硒在这里真的很重要。因此,我想创建一个程序,可以帮助我从Google上删除片段等内容,同时还可以使我能够自动执行其他某些任务的浏览器。这就是我所做的。

from selenium import webdriver as webd
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webd.Firefox()

driver.get('https://google.com/')
driver.find_element(By.NAME, 'q').send_keys('cars')
driver.find_element(By.NAME, 'q').send_keys(Keys.ENTER)
ques = driver.find_element(By.CLASS_NAME, "ULSxyf")
print(ques)

time.sleep(8)
driver.close()

尽管前几行效果很好。无论我做什么,我都无法打开的人也会用硒询问。我已经通过检查对象或使用ID等来使用该对象的类名称。在搜索一段时间后,我没有真正发现任何有助于这种特定情况方案的东西。我需要知道如何确切地执行此操作或为什么我的方法不起作用,如果有人有任何想法。如果你让我知道,我会很高兴。谢谢!

我是Selenium的总体初学者,因此,如果您不能给我一个直接的答案,但是觉得文章或教程会更好,那也会有所帮助。

编辑:我想打开的人也会问部分,并从那里提取答案以及问题本身。

Selenium is really important here. So, I want to create a program that can help me scrap stuff from google like the snippets etc while also giving me the ability to automate the browser for some other tasks. And here's what I've done.

from selenium import webdriver as webd
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webd.Firefox()

driver.get('https://google.com/')
driver.find_element(By.NAME, 'q').send_keys('cars')
driver.find_element(By.NAME, 'q').send_keys(Keys.ENTER)
ques = driver.find_element(By.CLASS_NAME, "ULSxyf")
print(ques)

time.sleep(8)
driver.close()

Although the first few lines works fine. I'm unable to open the People also ask section with selenium no matter what I do. I've used the class name of the object by inspecting it or used the id etc etc. And after searching for awhile I haven't really found anything much that would help this specific case scenario. I need to know how exactly to do this or why my method isn't working, if anybody has any idea. I'd be glad if you let me know. Thanks!

I'm a total beginner in selenium so if you can't give me a straight answer but feel a article or tutorial would be better, that would help as well.

EDIT: I want to open the questions from People also ask section and extract the answers from in there along with the questions themselves.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2025-02-15 16:15:10

要从 问题中提取文本 人们还会问列,您必须诱导 webdriverwait 使用以下任何一个 定位策略

  • P>代码块:

      driver.get(“ https://google.com/”)
    webdriverwait(驱动程序,20).unil(ec.element_to_be_clickable(((by.name,“ q”)))。send_keys(“ cars” +键。
    print([[my_elem.get_attribute(“ data-q”)for webdriverwait中的my_elem(driver,20).until(ec.visibility_of_all_elements_located(((by.xpath,” // spath,“ // span” /plost :: div [@data-q]”))))))))
     
  • 控制台输出:

      ['哪一辆最受欢迎的汽车?','什么是前10辆汽车?',哪种类型的汽车是最好的?','哪辆车最便宜?']
     
  • 注意:您必须添加以下导入:

     来自selenium.webdriver.support.ui导入webdriverwait
    从selenium.webdriver.common.通过进口
    从selenium.webdriver.support进口预期_conditions作为ec
     

To extract the texts from the questions under People also ask column you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

  • Code Block:

    driver.get("https://google.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("cars" + Keys.RETURN)
    print([my_elem.get_attribute("data-q") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text()='People also ask']//following::div[@data-q]")))])
    
  • Console Output:

    ['Which is the most popular car?', 'What are top 10 cars?', 'Which type of car is best?', 'Which car is very cheapest?']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文