使用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)