# 导包
from selenium import webdriver
import re
dr = webdriver.Chrome()
# 获取每页小说内容的函数
def get_page():
novel={}
novel['page_title'] = dr.find_element_by_xpath('//*[@id="BookCon"]/h1').text
novel['page_content'] = dr.find_element_by_xpath('//*[@id="BookText"]').text
return novel
url = 'https://www.2wxs.com/xstxt/312/118706.html' # 小说第一张的链接
dr.get(url) # 打开第一张小说链接
# 获取小说列表页url
list_url = dr.find_element_by_xpath('//*[@id="BookCon"]/div[1]/a[2]').get_attribute("href") # //*[@id="BookCon"]/div[1]/a[2]
print(list_url)
with open('斗罗大陆1.txt','a+',encoding='utf-8')as f:
while url != list_url: # 当url 不等于列表页url的时候一直循环。
content=get_page()
next_url = dr.find_element_by_xpath('//*[@id="BookCon"]/div[1]/a[3]')
url = next_url.get_attribute("href")
next_url.click()
# 写入文件
f.write(content['page_title'] + '\n')
f.write(content['page_content'] + '\n')
print(content)
dr.close()
初学爬虫-笔趣阁单本小说爬取(使用selenium)
最新推荐文章于 2024-05-08 21:32:09 发布