做阿里网站卖东西赚钱吗,建设网站用凡科怎么样,禅城区网站建站网站,东莞网络营销优化截止至2024.7.16
chrome浏览器最新版本为126.0.6478.127
但对应的chromeDriver版本都低于此版本#xff0c;因此#xff0c;转用Edge浏览器
说明#xff1a;仅记录自己使用过程中用到的一些代码和感受#xff0c;看具体情况不定期更新。 selenium官方文档
1、安装selen…截止至2024.7.16
chrome浏览器最新版本为126.0.6478.127
但对应的chromeDriver版本都低于此版本因此转用Edge浏览器
说明仅记录自己使用过程中用到的一些代码和感受看具体情况不定期更新。 selenium官方文档
1、安装selenium及下载EdgeDriver
pip install selenium
edgeDriver下载地址
2、在环境变量的path中放入EdgeDriver的路径
3、抓取
import random
from selenium import webdriver
from selenium.webdriver.common.by import ByUSER_AGENT_LIST [ Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/530.6 (KHTML, like Gecko) Chrome/2.0.174.0 Safari/530.6,Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.3.154.6 Safari/525.19,Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.599.0 Safari/534.13,Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.8 (KHTML, like Gecko) Chrome/7.0.521.0 Safari/534.8,Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1,Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5
]def user_agent():return random.choice(USER_AGENT_LIST)url 待爬网址
edge_options webdriver.EdgeOptions()
edge_options.use_chromium True # 指定使用基于Chromium的Edge浏览器
edge_options.add_argument(headless) # 无界面模式
edge_options.add_argument(disable-gpu) # 禁用GPU加速
#因为是遍历查询所以加入了随机user-agent
edge_options.add_argument(user-agentuser_agent())
driver webdriver.Edge(optionsedge_options)
driver.get(url) 4、常用代码
1、查找elements这返回的是数组如果确定唯一的则用find_element
links driver.find_elements(By.XPATH,//div[classjbxx-content]//a)
注语法跟xpath相似
2、获取WebElement的属性值
cos link.get_attribute(href)
3、执行click事件
#如果报ElementClickInterceptedException错误的话
driver.execute_script(arguments[0].scrollIntoView();,link)
#执行点击事件
link.click()
4、获取text值
text driver.find_element(By.ID, justanotherlink).text