from selenium import webdriver from selenium.common.exceptions import NoSuchElementException import time dr = webdriver.Chrome() # dr = webdriver.PhantomJS() def check_login(times): """ 校验登录是否成功 :param times: 超时时间,每秒检查一次 :return: """ n = 0 while n <= times: try: check_login = dr.find_element_by_class_name("site-nav-user").text if check_login != "": print("恭喜用户:%s 登录成功" %str(check_login).strip()) break except NoSuchElementException as msg: print("第%d次找不到元素" %n,msg) time.sleep(1) n += 1 def write_txt(key,pageNums): """ 写入csv文件 :param key:关键字 :param pageNums:最大页数 :return: """ with open("E://商品信息_%s.csv" % key, "w") as a: num = 1 while num <= pageNums: time.sleep(1) # 商品价格 sp_price = dr.find_elements_by_xpath("//div[@id='mainsrp-itemlist']//div[@class='items']/div
来源:CSDN
作者:lystest
链接:https://blog.csdn.net/lystest/article/details/103868429