第一只爬虫(selenium库的使用)

第一只爬虫,测试selenium库的使用,爬取一本书,按章节名进行保存

#!/usr/bin/env python3
"""
第一只爬虫,测试selenium库的使用,爬取一本书,按章节进行存盘
目标网页:https://www.xs1002.com/biquge43/102452/1274708.html
书名:首席御医(特喜欢此书,就用它开刀了!!!)
技术路线:Python,selenium
浏览器:Microsoft Edge
"""
import os
from selenium import webdriver
import time


def getHTMLtext(driver):
	"""获取章节内容"""
	comment = driver.find_element_by_id('booktext')
	sectionText = comment.text
	return sectionText


def saveText(chapterName, text):
	"""按章节名保存章节内容"""
	savePath = r"首席御医/{0}.txt"
	savePath = savePath.format(chapterName)

	file = open(savePath, "w")
	file.write(text)
	file.close()


def nextPage(driver):
	"""自动跳转至下一章"""
	time.sleep(2)  # 歇口气
	try:
		nextPage = driver.find_element_by_link_text('下一章')
		nextPage.click()
	except:
		errorChapter.append(driver.current_url)     # 记录无法打开的章节


def getChapterName(driver):
	"""获取章节名称"""
	return driver.find_element_by_tag_name('dt').text


if __name__ == "__main__":
	"""
	你可以从任意一章开始爬取,只需要改变url的值
	"""
	url = r"https://www.xs1002.com/biquge43/102452/1274708.html"

	chapterNum = 10  # 爬取的章节数量
	errorChapter = []  # 记录无法打开的章节
	bookName = "首席御医"
	driver = webdriver.Edge(r"C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe")
	driver.maximize_window()  # 最大化窗口

	try:
		driver.get(url)
	except:
		errorChapter.append(driver.current_url)

	for i in range(10):
		chapterText = getHTMLtext(driver)
		chapterName = "{0}-{1:0=4} ({2})".format(bookName, i+1, getChapterName(driver))
		saveText(chapterName, chapterText)

		nextPage(driver)

	driver.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值