Save web content to excel

from selenium import webdriver
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from openpyxl import Workbook

driver = webdriver.Chrome()
driver.get("https://tieba.baidu.com/index.html")

# Location search box
driver.find_element_by_xpath ( '// input [@ id = "wd1"]'). Send_keys ( " Sun Mao book")

Location search button #
driver.find_element_by_xpath ( '// a [text ( ) = " Full Search bar"]'). The Click ()
SLEEP (. 3)

# Open excel spreadsheet
wb = Workbook ()
WS = wb.active
ws.append ([ "title", "content"])
Row 2 =
ROW2 = 2

while True:
title_list = list()
try:
#定位标题
all_title = driver.find_elements_by_xpath('//span[@class="p_title"]')
all_content = driver.find_elements_by_xpath('//div[@class="p_content"]')
for title in all_title:
ws.cell(row, 1, title.text)
print("保存标题到excel表格中")
row = row + 1

Content in all_content for:
ws.cell (ROW2, 2, content.text)
Print ( "saving to excel format")
ROW2. 1 + = ROW2

wb.save("./data.xlsx")
# 定位下一页
driver.find_element_by_xpath('//a[@class="next"]').click()
sleep(3)

except NoSuchElementException as E:
break

Guess you like

Origin www.cnblogs.com/mary-ding/p/12190444.html