使用python下载小说

最近学习python,用来下载喜欢看的小说练手,直接上代码

# -*- coding:UTF-8 -*-
import requests
from bs4 import BeautifulSoup

def getdetail(text,url):
    target = 'https://www.bqgbi.cc'+url
    print(target)
    try:
        req = requests.get(url=target)
    except Exception as e:
        print("发送异常-->",str(e))
        return ""
    #print(req.text)
    bf = BeautifulSoup(req.text,"lxml")
    texts = bf.find(id="chaptercontent")
    #texts  = bf.find(attrs={'class':'ReadAjax_content'})
    #texts = bf.find_all("div",id="chaptercontent",class_="ReadAjax_content")
    
    readinline = bf.find(class_="readinline")
    if(readinline):
        readinline.decompose() #删除这个div
        #content = texts.getText().replace('\xa0'*8,'\n\n')

    #把<br/>替换为行号符
    for xx in texts:
       if(str(xx)=="<br/>"):
           #print("---",xx)
           xx.string = "\n"
  
    content = texts.getText()
    
    bf_content = BeautifulSoup(content)
 
    
    return content

if __name__ == '__main__':
    target = 'https://www.bqgbi.cc/book/876/'
    req = requests.get(url=target)
    #print(req.text)
    bf = BeautifulSoup(req.text,"lxml")
    texts = bf.find(class_="listmain")
    #print(texts)
    a_bf = BeautifulSoup(str(texts))
    alist = a_bf.find_all("a")
    #print(alist)
    file = open("F:/python_test/wp/修罗武神.txt", "w")
    for obj in alist:
        #print(obj)
        #print(obj.string,obj.get("href"))
        text = getdetail(obj.string,obj.get("href"))
        #print(obj.string)
        #print(text)
        if(text!=""):
            #file.writelines(obj.string)
            file.writelines(text.strip())
            file.write("\n")
        
        
    file.close



查看下载的文档:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值