【Python】利用lxml爬取起点小说网小说

先写在前面,人生苦短,我用python。

此文作为自己的一个小笔记,记录自己的爬虫的一些东西,此处为为爬取起点小说网的首页的全部小说,使用语言python,使用库lxml以及requests。

#!/usr/bin/env python
#!-*-coding:utf-8 -*-
#!@Time     :2018/12/7 21:11
#!@Author   :Guocc
#!@File     :GetStory.py

import os
from lxml import etree
import requests

class WebStoryGet(object):			# 定义网页抓取的类
     def __init__(self,story_url):
        self.url = story_url
    
    def start_request(self):			#定义抓取的准备步骤
            response = requests.get(self.url) #定义起点网站的url
            html = etree.HTML(response.content.decode())		#定义起点网站的内容解析
            Bigtitle_list = html.xpath('//div[@class="book-mid-info"]/h4/a/text()') #利用xpath提取小说标题
            Bighref_list = html.xpath('//div[@class="book-mid-info"]/h4/a/@href') #利用xpath提取小说的网址url
            for Bigtitle, Bighref in zip(Bigtitle_list, Bighref_list):
                self.section_get(Bigtitle, Bighref)

    def section_get(self, Bigtitle, Bighref):	#定义章节选择的函数
        section_content = requests.get("https:"+Bighref+"#Catalog")
        section_html = etree.HTML(section_content.content.decode())
        section_title_list = section_html.xpath('//div[@class="volume"]/ul/li/a/text()')
        section_href_list = section_html.xpath('//div[@class="volume"]/ul/li/a/@href')
        for section_title, section_href in zip(section_title_list, section_href_list):
            print(Bigtitle+" "+section_title+" 开始下载.\n")
            story = self.content_get(section_href)
            if not os.path.exists(Bigtitle) :
                os.mkdir(Bigtitle)

            with open (Bigtitle+"/"+section_title, "w",encoding="utf-8") as f:		#保存小说
                f.write(story)

    def content_get(self,section_href):		## 定义小说内容下载的函数
        story_response = requests.get("https:"+section_href)
        story_html = etree.HTML(story_response.content.decode())
        story = story_html.xpath('//div[@class="read-content j_readContent"]/p/text()')
        return ("\n".join(story))

if __name__ == '__main__':
	story_url = "https://www.qidian.com/all"
	Story = WebStoryGet(story_url)
	Story.start_request()

以上就是爬取起点小说网的源码,想爬其他网站可以修改story_url,不过如果小说的标题和网址的所在网页标签不同需修改xpath的信息以便提取网址url和标题,其他静态网页类似。

你可能感兴趣的:(【Python】利用lxml爬取起点小说网小说)