手动输入账号密码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.keys import Keys
import time
import xlwt
# 创建Excel
workbook=xlwt.Workbook()
sheet = workbook.add_sheet('工作表',cell_overwrite_ok=True)
sheet.write(0,0,"标题")
sheet.write(0,5,"价格")
sheet.write(0,7,'付款人数')
# 隐藏selenium
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--disable-blink-features=AutomationControlled")
web=webdriver.Chrome(options=option)
web.get('https://www.taobao.com/')
web.maximize_window()
web.implicitly_wait(10)
value=input('请输入你想要查询的物品名:')
web.find_element(By.XPATH,'//*[@id="q"]').send_keys(value,Keys.ENTER)
time.sleep(15) # 给手动输入账号密码时间
div=web.find_elements(By.XPATH,'//*[@id="mainsrp-itemlist"]/div/div/div[1]/div')
for i in range(1,len(div)+1):
price=web.find_element(By.XPATH,f'//*[@id="mainsrp-itemlist"]/div/div/div[1]/div[{i}]/div[2]/div[1]/div[1]/strong').text
numbers=web.find_element(By.XPATH,f'//*[@id="mainsrp-itemlist"]/div/div/div[1]/div[{i}]/div[2]/div[1]/div[2]').text
title=web.find_element(By.XPATH,f'/html/body/div[1]/div[2]/div[3]/div[1]/div[21]/div/div/div[1]/div[{i}]/div[2]/div[2]/a').text
sheet.write(i,0,title)
sheet.write(i,5,price)
sheet.write(i,7,numbers)
web.implicitly_wait(10)
print("over!!!")
workbook.save('淘宝.xls')
print("all over!!!")
input() # 防止突然关闭
web.close()
数据保存在Excel表格中
|