python-selenium-运行js代码--下拉操作

使用python-selenium运行js代码完成下拉操作:在某些情况,当我们下拉到浏览器,浏览器才会给我们加载数据,所有我们需要js带代码

driver.execute_script("""
写入你要执行的js代码
""")

下拉的js代码:

# document.documentElement.scrollTop指定滚动条的位置
# document.documentElement.scrol1Height 获取当前页面的最大高度
#'document.documentElement.scrol1Tdp=1000
#js_all = 'document.documentElement.scrollTop=document.documentElement.scrollHeight'
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.chrome.options import Options
#取消浏览器中的自动化程序正在控制浏览器
opt = Options()
opt.add_experimental_option('excludeSwitches',['enable-automation'])

# 1:打开浏览器
driver = webdriver.Chrome(options=opt)
# 2:输入你的网站
driver.get('https://search.jd.com/Search?keyword=python&enc=utf-8&wq=python&pvid=f7852fe54b664497b8bbf61060df3aa8')
driver.implicitly_wait(30)
#使用selenium执行js代码---完成下拉动作
driver.execute_script("""
document.documentElement.scrollTop=document.documentElement.scrollHeight
""")


time.sleep(100)

# document.documentElement.scrollTop指定滚动条的位置
# document.documentElement.scrol1Height 获取当前页面的最大高度
#'document.documentElement.scrol1Tdp=1000
#js_all = 'document.documentElement.scrollTop=document.documentElement.scrollHeight'

也可以进阶一下,分别下拉5次,使他的动作更加想人

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.chrome.options import Options
#取消浏览器中的自动化程序正在控制浏览器
opt = Options()
opt.add_experimental_option('excludeSwitches',['enable-automation'])
def xiala():
    """执行下拉操作"""
    for i in range(1,11,2):#1 3 5 7 9
        time.sleep(0.5)
        j=i/9 #1/9 3/9 5/9  7/9 9/9    可以下拉5次
        js_all="document.documentElement.scrollTop=document.documentElement.scrollHeight * %f" %j  #%f百分号的占位符
        driver.execute_script(js_all)
# 1:打开浏览器
driver = webdriver.Chrome(options=opt)
# 2:输入你的网站
driver.get('https://search.jd.com/Search?keyword=python&enc=utf-8&wq=python&pvid=f7852fe54b664497b8bbf61060df3aa8')
driver.implicitly_wait(30)
#使用selenium执行js代码---完成下拉动作
xiala()



time.sleep(100)

# document.documentElement.scrollTop指定滚动条的位置
# document.documentElement.scrol1Height 获取当前页面的最大高度
#'document.documentElement.scrol1Tdp=1000
#js_all = 'document.documentElement.scrollTop=document.documentElement.scrollHeight'
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.chrome.options import Options
import csv
#取消浏览器中的自动化程序正在控制浏览器
opt = Options()
opt.add_experimental_option('excludeSwitches',['enable-automation'])
def xiala():
    """执行下拉操作"""
    for i in range(1,11,2):#1 3 5 7 9
        time.sleep(0.5)
        j=i/9 #1/9 3/9 5/9  7/9 9/9    可以下拉5次
        js_all="document.documentElement.scrollTop=document.documentElement.scrollHeight * %f" %j  #%f百分号的占位符
        driver.execute_script(js_all)

def asd():
    content_list=driver.find_elements(By.XPATH,'//*[@id="J_goodsList"]/ul/li')
    for a in content_list:
        name=a.find_element(By.XPATH,".//div/div[3]/a").text
        money=a.find_element(By.XPATH,".//div/div[2]/strong/i").text
        evaluate = a.find_element(By.XPATH, ".//div/div[4]/strong/a").text
        publishing_house = a.find_element(By.XPATH, ".//div/div[5]/span/a").text
        with open('购物商品.csv', mode='a+', encoding='utf-8', newline='') as f:
            wirter = csv.writer(f)  # 创建一个写入的对象
            if a=="python" and money=="69.80":
                wirter.writerow(['书名', '价格', '评价人数(万)', "出版社"])  # 设置第一行标
            wirter.writerow([name,money,evaluate,publishing_house])
        print(f"{name}下载完毕")


# 1:打开浏览器
driver = webdriver.Chrome(options=opt)
# 2:输入你的网站
a=input("请输入你要搜索的内容:")
driver.get(f'https://search.jd.com/Search?keyword={a}')
driver.implicitly_wait(30)
#使用selenium执行js代码---完成下拉动作
xiala()
#准备获取东西信息
asd()
time.sleep(100)




 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值