使用Selenium WebDriver检查断开的链接并将结果保存到Excel中,可以按照以下步骤进行操作:
from selenium import webdriver
from selenium.webdriver.common.by import By
from openpyxl import Workbook
driver = webdriver.Chrome() # 使用Chrome浏览器,需要提前安装ChromeDriver并配置环境变量
driver.get("http://example.com") # 替换为你要检查的网页URL
links = driver.find_elements(By.TAG_NAME, "a")
results = []
for link in links:
url = link.get_attribute("href")
try:
driver.get(url)
if driver.title: # 如果链接未断开,获取页面标题
results.append((url, "正常"))
else:
results.append((url, "断开"))
except:
results.append((url, "断开"))
wb = Workbook()
ws = wb.active
ws.append(["链接", "状态"])
for result in results:
ws.append(result)
wb.save("link_status.xlsx") # 保存为link_status.xlsx文件
完整代码示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
from openpyxl import Workbook
driver = webdriver.Chrome()
driver.get("http://example.com")
links = driver.find_elements(By.TAG_NAME, "a")
results = []
for link in links:
url = link.get_attribute("href")
try:
driver.get(url)
if driver.title:
results.append((url, "正常"))
else:
results.append((url, "断开"))
except:
results.append((url, "断开"))
wb = Workbook()
ws = wb.active
ws.append(["链接", "状态"])
for result in results:
ws.append(result)
wb.save("link_status.xlsx")
driver.quit()
这样,你就可以使用Selenium WebDriver检查断开的链接并将结果保存到Excel中了。请注意,以上代码示例中使用的是Chrome浏览器和ChromeDriver,你需要根据自己的实际情况选择合适的浏览器和对应的WebDriver。
领取专属 10元无门槛券
手把手带您无忧上云