Selenium

Selenium是一个强大的自动化测试工具,它支持多种浏览器,包括IE、Mozilla Firefox、Safari、Google Chrome、Opera、Edge等。它最初是为Web应用程序的自动化测试而创建的,但后来也被广泛用于进行网络数据抓取和网页内容爬取,特别是那些需要JavaScript渲染的页面。Selenium提供了多种编程语言的绑定,包括Python、Java、C#、JavaScript等,使开发人员能够使用自己熟悉的编程语言来编写自动化脚本。它的核心组件包括WebDriver(提供了一组API来操作各种浏览器),Selenium IDE(一个用于浏览器自动化的集成开发环境,允许非开发人员录制和回放操作来创建自动化脚本),以及Selenium Grid(允许同时在多个浏览器和操作系统上运行测试)。

chrome_service = Service(ChromeDriverManager().install())

chrome_service = Service(ChromeDriverManager().install())是在使用Selenium库来管理Chrome浏览器的驱动。

首先,ChromeDriverManager()返回一个ChromeDriverManager对象,这个对象可以用于获取和安装ChromeDriver。ChromeDriver是一个用于与Chrome浏览器交互的二进制文件,它允许你通过编程方式控制Chrome浏览器。

然后,install()方法会返回一个WebDriver的实例,这个实例会启动一个ChromeDriver服务,并返回一个代表此服务的Service对象。这个Service对象可以用于管理这个服务,例如让它停止运行。

所以,这行代码的主要作用是启动一个ChromeDriver服务,并将其封装在Service对象中,以便后续管理。之后你就可以使用这个Service对象来启动和关闭ChromeDriver服务。在运行过程中会自动下载相关的webdriver,同时你要选安装好chrome

网站调试

打开网站https://ft.amtb.cn,如下图 分析网页源码:
python 能过selenium跟微信交互操作1-LMLPHP
选择简体的html代码
单选框

     <span id="ctl00_RBL" style="color:#404040">
                                <input id="lang_cn" type="radio" name="lang" value="zh_CN" checked='checked' /> 简体
                                <input id="lang_tw" type="radio" name="lang" value="zh_TW" /> 正體
     </span>

查询的文本框

  <input name="query" type="text" id="query" value="" title="AND Logic請用空白格開 ^_^" onkeypress="return editKeyHandler(event)" />

搜寻安钮

   <input type="button" name="searchButton" id="searchButton" accesskey="S" title="ALT+S可以直接執行搜索" value="搜尋" />

简单的代码

from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_service = Service(ChromeDriverManager().install())
from bs4 import BeautifulSoup
options = webdriver.ChromeOptions()
options.add_argument('--no-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')

# Use the Service object when creating the WebDriver
driver = webdriver.Chrome(service=chrome_service, options=options)
driver.get('https://ft.amtb.cn')
# Find the element with ID "ctl00_RBL" on the page
span = driver.find_element(by=webdriver.common.by.By.ID, value="ctl00_RBL")
# Find the radio button with ID "lang_cn" within the "span" element
radio_cn = span.find_element(by=webdriver.common.by.By.ID, value="lang_cn")
radio_cn.click()
# Find the input element with ID "query" on the page
input_element =  driver.find_element(by=webdriver.common.by.By.ID, value="query")
input_element.send_keys("三界外")
# Find the search button with ID "searchButton" on the page
search_button = driver.find_element(by=webdriver.common.by.By.ID, value="searchButton")
search_button.click()
sleep(10)
# Wait for the results to load (you might want to add an explicit wait here)

# Get the updated page source after the search
page_source = driver.page_source
print(page_source)
# Close the browser window when done
soup = BeautifulSoup(page_source, 'lxml')
article = soup.find('article')
text = article.get_text()
print(text)
driver.quit()

BeautifulSoup引入进行格式化

以上的代码为了不弹出浏览器

options = webdriver.ChromeOptions()
options.add_argument('--no-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--headless')

打开网站当然看不见。如果driver = webdriver.Chrome(service=chrome_service)则可以看到弹出相关的网页。

driver = webdriver.Chrome(service=chrome_service, options=options)
driver.get('https://ft.amtb.cn')

选中简体,这里的click有一个选择的动作

# Find the element with ID "ctl00_RBL" on the page
span = driver.find_element(by=webdriver.common.by.By.ID, value="ctl00_RBL")


# Find the radio button with ID "lang_cn" within the "span" element
radio_cn = span.find_element(by=webdriver.common.by.By.ID, value="lang_cn")
radio_cn.click()

给文本查询框赋值

input_element =  driver.find_element(by=webdriver.common.by.By.ID, value="query")
input_element.send_keys("三界外")

点击窗口的功能:这里等二秒让查询界面的查询显示出来,时间需要自已调整。

search_button = driver.find_element(by=webdriver.common.by.By.ID, value="searchButton")
search_button.click()
sleep(2)
         <div class="row justify-content-center align-items-center">
                                <div class="col-12 col-lg-10 col-xl-9">
                                    <article style="font-size:1.125rem; background-color: #fafafa">
                                        <p>普贤行愿品别行疏钞  (第五十六集)  1989/1  台湾华藏讲堂  档名:04-003-0056</p>
<p>  请掀开经本,第一百七十六面,倒数第四行第六个字看起:</p>
<p>  【三约喻者。佛如良医。法如良药。僧宝如看病人。】</p>
<p>  这是从比喻上来说。众生可以说都是病人,病了, ……</p>                                    </article>
                                </div>
                            </div>

所以我们解析一下查询结果查询到第一个结果。driver.quit()闭网页 。注意如果find方法查找不到时 要做异常处理

page_source = driver.page_source
# Close the browser window when done
soup = BeautifulSoup(page_source, 'lxml')
article = soup.find('article')
text = article.get_text()
print(text)
driver.quit()

12-13 06:24