码迷,mamicode.com
首页 > 其他好文 > 详细

起点中文网小说爬取-etree,xpath,os

时间:2019-02-20 22:49:42      阅读:760      评论:0      收藏:0      [点我收藏+]

标签:start   art   应用   list   .text   ==   tps   div   big   

本文章主要是lxml库的etree解析抽取与xpath解析的应用,还使用了os库写文件

import os
import requests
from lxml import etree#lxml库解析HTML、xml文件抽取想要的数据
#设计模式--面向对象
class Spider(object):
    def start_request(self):
        #1.请求网站拿到数据,抽取小说名创建文件夹,抽取
        response=requests.get(‘https://www.qidian.com/all‘)
        # print(response.text)
        html=etree.HTML(response.text)#结构化
        Bigsrc_list=html.xpath(‘//div[@class="book-mid-info"]/h4/a/@href‘)
        #//:选取元素,而不考虑元素的具体位置;     @:选取属性
        Bigtit_list=html.xpath(‘//div[@class="book-mid-info"]/h4/a/text()‘)
        # print(Bigsrc_list,Bigtit_list)
        for Bigsrc,Bigtit in zip(Bigsrc_list,Bigtit_list):
            if os.path.exists(Bigtit)==False:#如果不存在文件夹名为该小说名,就创建
                os.mkdir(Bigtit)
            # print(Bigsrc,Bigtit)
            self.file_data(Bigsrc,Bigtit)#调用下一个函数

    def file_data(self, Bigsrc, Bigtit):
        # 2.请求小说,拿到数据,抽取章名,抽取文章链接
        response = requests.get("https:" + Bigsrc)#补上缺少的https前缀
        html = etree.HTML(response.text)#etree.HTML可用于在python代码中嵌入“html文本”。
        listsrc_list = html.xpath(‘//ul[@class="cf"]/li/a/@href‘)
        listtit_list = html.xpath(‘//ul[@class="cf"]/li/a/text()‘)
        for Listsrc, Listtit in zip(listsrc_list, listtit_list):
            # print(Listsrc, Listtit)
            self.finally_file(Listsrc, Listtit,Bigtit)

    def finally_file(self,Listsrc, Listtit,Bigtit):
        # 3.请求文章拿到抽取文章内容,创建文件保存到相应文件夹
        response=requests.get("https:"+Listsrc)
        html=etree.HTML(response.text)#结构化
        content="\n".join(html.xpath(‘//div[@class="read-content j_readContent"]/p/text()‘))
        #S.join()返回一个字符串,元素之间的分隔符是S
        file_name=Bigtit+"\\"+Listtit+".txt"
        #创建Bigtit文件夹下的Listtit.txt文件
        print("正在存储文件"+file_name)
        with open(file_name,"a",encoding="utf-8")as f:
            f.write(content)
if __name__==‘__main__‘:
    spider = Spider()
    spider.start_request()

  

起点中文网小说爬取-etree,xpath,os

标签:start   art   应用   list   .text   ==   tps   div   big   

原文地址:https://www.cnblogs.com/fodalaoyao/p/10409736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!