#案例:web自动化的一个案例!字节跳动!写到csv文件中!
from selenium import webdriver # 导入webdriver包
import time
driver=webdriver.Chrome()
driver.maximize_window() # 最大化浏览器
driver.implicitly_wait(30)
driver.get("https://baike.baidu.com/item/%E5%AD%97%E8%8A%82%E8%B7%B3%E5%8A%A8%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8/19921953?fr=aladdin")
#
# driver.find_element_by_id("kw").send_keys("百度百科字节跳动")
# driver.find_element_by_id("su").click()
# driver.find_element_by_css_selector("[data-showurl-highlight='false']").click()
# driver.switch_to.window(driver.window_handles[-1])
#解析发展历程
ele_div=driver.find_element_by_css_selector("div.main-content")
eles_div=ele_div.find_elements_by_css_selector("div[class='para'][data-pid]")
for div in eles_div:
attr_data_pid=div.get_attribute("data-pid")
t1=int(attr_data_pid)
if t1>=5 and t1<=30:
text=div.text
t2=text.split(",",1)
time=t2[0]
sijian=t2[1]
t3=time+","+sijian
with open("C:/A/haha.csv","a") as fp:
fp.write(t3)
fp.write("\n")
time.sleep(5)
driver.quit() # 关闭并退出浏览器