当前位置:   article > 正文

selenium+爬虫 实现百度搜索,返回相关查询结果_访问某个网址返回搜索结果脚本

访问某个网址返回搜索结果脚本

主要实现的功能:

   使用selenium访问百度网站,输入【关键字】,点击 搜索,获取到页面信息 与 关键字 进行对比。

from selenium import webdriver
import time

def baidu_Search():
    keyword="python"
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.baidu.com/")
    # 输入【关键字】
    driver.find_element_by_id("kw").send_keys(keyword)
    # 点击【搜索】
    driver.find_element_by_id("su").click()
    time.sleep(3)

    ## 获取查询列表的 个数
    list= driver.find_elements_by_class_name("c-container")
    # print(""+len(list))
    # 根据xpath 获取 c-container内(a标签text )所有标题名称
    for i in range(1,len(list)+1):
        text=driver.find_element_by_xpath('//*[@id="'+str(i)+'"]/h3/a').text
        if(text.lower().find(keyword)>=0):
            print("查询结果中包含关键字","查询结果标题:",text)
        else:
            print("查询结果中不包含关键字", "查询结果标题:", text)

    time.sleep(3)
    driver.close()

baidu_Search()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/768260
推荐阅读
  

闽ICP备14008679号