python 代码模拟事件

参考xpath1
参考xpath2
参考xpath3

环境搭建

安装

pip install selenium
pip install beautifulsoup4

还不行,可试一下下载安装到当前运行的编辑器下的包文件夹里

pip install --target=E:\AZ\python\ANACONDA\envs\py38\Lib\site-packages  beautifulsoup4
pip install --target=E:\AZ\python\ANACONDA\envs\py38\Lib\site-packages selenium

下载

Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

推荐下载chromedriver地址
下载chromedriver地址:https://chromedriver.chromium.org

chromedriver.exe文件放至python.exe所在目录

以下错误说明下载版本不对,要下载102.0.5005才行

 Message: session not created: This version of ChromeDriver only supports Chrome version 103      
Current browser version is 102.0.5005.115 with binary path C:\Users\CFFHL\AppData\Local\Google\Chrome\Application\chrome.exe
Stacktrace:

参考代码

from bs4 import BeautifulSoup
from selenium import webdriver
target = '网页网址'
option = webdriver.ChromeOptions()
option.add_argument('headless')  # 设置option,后台运行
driver = webdriver.Chrome(chrome_options=option)
driver.get(target)

result= driver.find_element_by_class_name('需要点击的类名')
result.click()


result_list= driver.find_elements_by_class_name('需要点击的类名')
for i in range(4, 8):
    result_list[i].click()


selenium_page = driver.page_source
driver.quit()
soup = BeautifulSoup(selenium_page, 'html.parser')
# one = soup.find('div', {'class': '布拉布拉类名'}) 单个
many= cities.find_all('div', {'class': '咕噜咕噜类名'})  #多个
for i in many:
        content = i.find_all('p') #找到对应元素
        nation = content[0].get_text() # 读取内容


以分析百度首页为例

from bs4 import BeautifulSoup
from selenium import webdriver
target = 'https://www.baidu.com/'
option = webdriver.ChromeOptions()
option.add_argument('headless')  # 设置option,后台运行
driver = webdriver.Chrome(chrome_options=option)
driver.get(target)

result= driver.find_element_by_class_name('bg s_btn btnhover')
result.click()


result_list= driver.find_elements_by_class_name('bg s_btn btnhover')
for i in range(4, 8):
    result_list[i].click()


selenium_page = driver.page_source
driver.quit()
soup = BeautifulSoup(selenium_page, 'html.parser')
print(soup)
# one = soup.find('div', {'class': '布拉布拉类名'}) 单个
# many= soup.find_all('div', {'class': '咕噜咕噜类名'})  #多个
# for i in many:
#         content = i.find_all('p') #找到对应元素
#         nation = content[0].get_text() # 读取内容

出现以下,或者正确结果,说明 环境搭建成功

AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'

加入

from selenium.webdriver.common.by import By

把元素选取改为


result_list= driver.find_element(By.XPATH,r'网页元素复制过来的').click()

控制台调试

$x('网页元素复制过来的xpath')

以下错误可忽略:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
requests 2.22.0 requires urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1, but you have urllib3 1.26.9 which is incompatible.
pip list
pip uninstal requests
pip install reques

参考

参考1
参考2
参考3
参考4

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wx_h13813744

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值