使用selenium爬取数据并存储到数据库

爬取小说

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import TimeoutException
import pymongo

# 将chromedriver.exe放在当前py同一目录
driver = Chrome(service=Service(executable_path=r"chromedriver.exe"))
# 对比得出小说总长度为1-2363
# https://www.00ksw.com/html/1/1170/14041.html
# https://www.00ksw.com/html/1/1170/14042363.html
url = "https://www.00ksw.com/html/1/1170/14041.html"
# 打开要爬取小说的网页的第一页
driver.get(url)
# 等待10秒时间。如果在10秒内找到元素,则立即继续执行;
# 如果10秒内没有找到元素,将会抛出一个 NoSuchElementException 异常
# driver.implicitly_wait(10)
# 设置页面加载超时时间为30秒
driver.set_page_load_timeout(30)
content=[]
# 连接创建数据库与集合
client = pymongo.MongoClient()
db = client.get_database("lingdian")
collection=db.get_collection("蛊真人")
# 循环爬取所有章节
i = 1
while i < 2364:
    print(f"正在爬取第{i}页")
    # 提取每章的标题名
    title = driver.find_element(By.XPATH, '//*[@id="nr_content"]/div[2]/h3').text
    # 提取每章的段落内容
    datas = driver.find_elements(By.XPATH, '//*[@id="articlecontent"]/p')
    # 存放段落内容的文本信息
    text=[]
    for data in datas:
        text.append(data.text)
    # 每提取一章就存入数据库一章
    collection.insert_one({"title":title,"datas":text})
    try:
        # 点击下一章
        driver.find_element(By.XPATH, '// div[ @ id = "nr_content"] / div[7] / a[4]').click()
    except TimeoutException:
        driver.get(f"https://www.00ksw.com/html/1/1170/1404{i+1}.html")
    i += 1
# 关闭数据库
client.close()
print("爬取结束")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值