0


python用selenium模拟谷歌浏览器点页面

1、cmd安装selenium,输入pip install selenium

2、模拟点击热搜第一条进去,连接如下

https://weibo.com/newlogin?tabtype=weibo&gid=102803&openLoginLayer=0&url=https%3A%2F%2Fweibo.com%2F

3、查看谷歌版本

4、并去下面下载对应版本的webdriver,解压后把chromedriver.exe放入python目录

CNPM Binaries Mirrorhttps://registry.npmmirror.com/binary.html?path=chromedriver/

3、写代码测试

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

option = webdriver.ChromeOptions()
#此配置去掉浏览器正在受自动软件的监控
option.add_experimental_option('excludeSwitches', ['enable-automation'])
#模拟浏览器点击
option.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"')
#设置信息
driver = webdriver.Chrome(options=option)
#浏览器最大化,不放大有可能页面会兼容手机隐藏掉,之前小窗口老是xpath获取不到数据
driver.maximize_window()
# 打开chrome浏览器
driver.get("https://weibo.com/newlogin?tabtype=weibo&gid=102803&openLoginLayer=0&url=https%3A%2F%2Fweibo.com%2F/")
#网页打开要等待全部加载完才能获取到节点
time.sleep(15)
#获取微博热搜第一个点进去
hots = driver.find_elements(By.XPATH,"//div[@class='wbpro-side-card7']/div[@class='wbpro-side-panel']/a");
for hot in hots:
    hot.click()
    break;

print(driver.current_url)

time.sleep(5)
driver.quit()

4、完事了~~~后面持续更新


本文转载自: https://blog.csdn.net/wxs55555/article/details/131831388
版权归原作者 wxs55555 所有, 如有侵权,请联系我们删除。

“python用selenium模拟谷歌浏览器点页面”的评论:

还没有评论