栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 代码模拟事件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 代码模拟事件

环境搭建 安装
pip install selenium
pip install beautifulsoup4

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

pip install --target=E:AZpythonANACONDAenvspy38Libsite-packages  beautifulsoup4
pip install --target=E:AZpythonANACONDAenvspy38Libsite-packages selenium
下载
Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

下载chromedriver地址

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:UsersCFFHLAppDataLocalGoogleChromeApplicationchrome.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'

以下错误可忽略:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994810.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号