【问题标题】:Unable to Google Search Successfully with Selenium无法使用 Selenium 成功进行 Google 搜索
【发布时间】:2021-05-03 13:14:33
【问题描述】:

我正在尝试使用 Selenium 进行基本的谷歌搜索。这是非常基本的代码,但仍然没有返回任何搜索结果

!pip install kora -q
from kora.selenium import wd
import time
from bs4 import BeautifulSoup as BS

def get_results(search_string): 
  url = 'https://www.google.com/'
  wd.get(url)
  time.sleep(3)
  subject_box = wd.find_element_by_css_selector('input[name=q]')
  subject_box.send_keys('search_string')
  subject_box.submit()
  time.sleep(3)
  html2 = wd.page_source 

  print(html2)

get_results('dog')

我的输出如下,我在任何地方都找不到搜索词 dog - 不止这些,但你明白了:

输出 1:

输出 2:

关于为什么会发生这种情况的任何建议?

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver beautifulsoup


    【解决方案1】:
    subject_box.send_keys('search_string')
    

    改成

    subject_box.send_keys(search_string)
    

    您没有传递变量,而只是传递了一个字符串 'search_string' ,当引号被删除时,它会从变量中传递值 dog

    【讨论】:

    • 请按勾号接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多