欢迎光临 - 我的站长站,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

python教程

笔趣阁小说搜索+txt下载Python爬虫

python教程 我的站长站 2022-01-07 共227人阅读

第三方库需求: Faker

安装方法:终端输入pip3 install faker
不安装将相关代码删除自行添加假UA也可使用。

使用方法:下载源码直接运行即可。

功能介绍:一键搜索+下载,下载的txt文件会自动查重,已经存在的不会重复下载浪费时间和存储空间。

文件下载位置:程序执行同目录下会自动创建小说文件夹,每次下载小说都会在里面添加一个以小说书名命名的文件夹,内容分章存储。

# -*- coding: utf-8 -*- 
"""
Project Name: novel
Author: ARtcgb
Email:artcgb@ebay.onmicrosoft.com
Date: 2021/4/24
"""
import os
import requests
from faker import Faker
from bs4 import BeautifulSoup
f = Faker()
headers = {'user-agent': f.user_agent()}
while True:
    search_url = "https://www.mibaoge.com/search.php?q=" + input("输入要查找的书名:")
    requests_url = requests.get(search_url, headers=headers)
    soup_url = BeautifulSoup(requests_url.text, "lxml")
    search_list = soup_url.find_all("a", cpos="title")
    if search_list:
        count = 1
        for soup in search_list:
            print(count, soup.text.replace("n", ""))
            count += 1
        break
    else:
        print("未搜索到相关结果")
choose = int(input("请输入所选查找结果前面的数字:")) - 1
title = soup_url.find_all("a", cpos="title")[choose].text.replace("n", "")
title_url = soup_url.find_all("a", cpos="title")[choose]["href"]
url = "https://www.mibaoge.com" + title_url
requests_url = requests.get(url, headers=headers)
soup_url = BeautifulSoup(requests_url.text, "lxml")
menu_list = soup_url.find("div", id="list").find_all("a")
url_list = []
chapter_list = []
for menu in menu_list:
    url_list.append(menu['href'])
    chapter_list.append(menu.text)
print("共找到", len(url_list), "个章节")
a = os.path.exists("./小说")
b = os.path.exists("./小说/" + title)
if a and b:
    print("文件夹已存在,PASS")
elif a:
    os.mkdir("./小说/" + title)
    print("文件夹建立成功")
else:
    os.mkdir("./小说")
    os.mkdir("./小说/" + title)
    print("文件夹建立成功")
url_count = 0
for url in url_list:
    a = os.path.exists("./小说/" + title + "/" + chapter_list[url_count] + ".txt")
    url_count += 1
    if a:
        try:
            print(chapter_list[url_count], "已存在,PASS")
        except IndexError:
            print("全部章节验证完毕")
        continue
    requests_url = requests.get("https://www.mibaoge.com/" + url, headers=headers)
    soup = BeautifulSoup(requests_url.text, "lxml")
    chapter = soup.find("h1").text
    content = str(soup.find("div", id="content")).replace("<!--go-->", "").replace("<!--over-->", "") 
        .replace("<br/>", "n").replace("<div id="content">", "").replace("</div>", "")
    with open("./小说/" + title + "/" + chapter + ".txt", "w") as f:
        f.write(content)
        print(chapter, "写入成功")
print(title, "写入成功")


标签 python爬虫
相关推荐
  • python爬虫
  • Python好看视频地址解析下载代码

    #encoding:utf-8# 好看视频下载 import socketfrom urllib.request import urlopenimport urllibimport reimport timefrom pyquery import PyQuery as pqimport requestsfrom tqdm import tqdm # 打印进度条的库import gzip print(&#39;程序开始运...

    python教程 101 2年前
  • python美女写真图库爬虫

    import requestsfrom lxml import etreeimport csvfrom time import sleepimport osfrom concurrent.futures import ThreadPoolExecutor headers = { &#39;user-agent&#39;: &#39;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit...

    python教程 43 2年前
  • 笔趣阁小说网Python爬虫分享

    #[url=https://www.biquge.info/wanjiexiaoshuo/]https://www.biquge.info/wanjiexiaoshuo/[/url] 笔趣阁小说全本爬虫import timeimport requestsimport osimport randomfrom lxml import etreeimport webbrowserheader = { "User-Agent": "Mo...

    python教程 140 2年前
  • Python爬取站长之家端口扫描接口

    import requests,timeimport randomfrom bs4 import BeautifulSoupimport reimport threadingdef ports(hostm,port): url = &#39;http://tool.chinaz.com/port/&#39; headers = { &#39;User-Agent&#39;:&#39;Mozilla/5.0 (Windows NT ...

    python教程 76 2年前
  • python爬虫下载抖音用户所有短视频+无水印方法

    这次分享下载抖音用户所有短视频方法,python爬虫批量抓取,无水印下载,希望和大家多多交流互相学习!获取用户链接方法1、首先在抖音上随机挑选一个小姐姐,用户主页右上角点开,获取分享链接python下载抖音视频截图得到类似分享链接:在抖音,记录美好生活! https:...

    python教程 250 2年前