python 爬虫 - 静态

#-*-coding:utf-8-*-
# coding:utf-8

#导入模块
import os
import requests
from pyquery import PyQuery as body
#抓取地址
url = "https://www.xbiquge.la"
# 抓取小说 [笔趣阁] 函数
def xiaosuo():
	#判断目录xiaosuo是否存在
    if not os.path.isdir(os.getcwd() + '/xiaosuo'):
    	#不存在则创建
        os.mkdir(os.getcwd()+'/xiaosuo')
	#获取源码并转成jquery语法
    jquery = body(requests.get(url + '/xiaoshuodaquan/').content, parser='html')
    #获取目录所有(小说书名) 地址
    for a in jquery('.novellist').find('ul').find('li').items():
    	#判断目录(小说书名) 是否存在
        if not os.path.isdir(os.getcwd() + '/xiaosuo/' + a.find('a').text()):
        	#不存在则创建
            os.mkdir(os.getcwd() + '/xiaosuo/' + a.find('a').text())
        #抓取每本小说
        jquery = body(requests.get(a.find('a').attr('href')).content, parser='html')
        #获取每本小说
        for b in jquery('#list dd').items():
        	#获取源码并转成jquery语法格式
            jquery = body(requests.get(url + b.find('a').attr('href')).content, parser='html')
            #写入文本
            open(os.getcwd() + '/xiaosuo/' + a.find('a').text() + '/' + b.find('a').text() + '.txt', 'w').write(jquery('#content').remove('p').text())
#调用函数            
xiaosuo()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值