Python中如何编写阅读小说、漫画的脚本(适用于iOS Pythonista APP)

https://github.com/yjqiang/yj_ebook_reader
1.代码写了挺久了,大改了好几次,还是不满意,但是暂时没有什么更好的办法了,欢迎大佬们提意见或者直接喷也可以
2.代码仅支持 ios pythonista app
3.里面除了 beautifulsoup4 之外,还有一个 toml 的依赖包,用 stash 都能安装
4.代码还在修改,希望大家不吝赐教


Python中如何编写阅读小说、漫画的脚本(适用于iOS Pythonista APP)

5 回复

最近趁低价和即将更新买了 Pythonista,在写一个管理 zotero 的脚本,可以交流一下(我并不是程序员


在Pythonista里写个小说/漫画阅读脚本,核心就三件事:下载、解析、展示。

1. 基础框架

import requests
import re
from bs4 import BeautifulSoup
import console
import ui

class ReaderApp:
    def __init__(self):
        self.chapters = []
        self.current_chapter = 0
        
    def fetch_content(self, url):
        """抓取网页内容"""
        headers = {'User-Agent': 'Mozilla/5.0'}
        try:
            resp = requests.get(url, headers=headers, timeout=10)
            resp.encoding = 'utf-8'
            return resp.text
        except:
            return None
    
    def parse_chapters(self, html):
        """解析章节列表 - 需要根据具体网站调整"""
        soup = BeautifulSoup(html, 'html.parser')
        # 示例:查找所有章节链接
        links = soup.find_all('a', href=re.compile(r'/chapter/'))
        self.chapters = [{'title': link.text, 'url': link['href']} for link in links]
    
    def parse_content(self, html):
        """解析正文内容 - 需要根据具体网站调整"""
        soup = BeautifulSoup(html, 'html.parser')
        content_div = soup.find('div', class_='content')
        if content_div:
            # 清理无关标签
            for tag in content_div.find_all(['script', 'style', 'iframe']):
                tag.decompose()
            return str(content_div)
        return "<p>内容解析失败</p>"
    
    def show_reader(self):
        """创建阅读界面"""
        view = ui.View(frame=(0, 0, 400, 600))
        webview = ui.WebView(frame=(0, 0, 400, 600))
        view.add_subview(webview)
        
        if self.chapters:
            content = self.parse_content(self.fetch_content(self.chapters[0]['url']))
            webview.load_html(content)
        
        view.present('fullscreen')

2. 漫画阅读示例

class ComicReader(ReaderApp):
    def parse_image_urls(self, html):
        """解析漫画图片地址"""
        soup = BeautifulSoup(html, 'html.parser')
        img_tags = soup.find_all('img', {'class': 'comic-page'})
        return [img['src'] for img in img_tags if 'src' in img.attrs]
    
    def show_comic(self):
        """显示漫画页面"""
        view = ui.ScrollView(frame=(0, 0, 400, 600))
        y = 10
        
        if self.chapters:
            html = self.fetch_content(self.chapters[0]['url'])
            image_urls = self.parse_image_urls(html)
            
            for url in image_urls:
                img_view = ui.ImageView(frame=(10, y, 380, 500))
                # 这里需要实现图片下载和缓存
                view.add_subview(img_view)
                y += 510
        
        view.content_size = (400, y)
        view.present('fullscreen')

3. 使用示例

# 小说阅读
app = ReaderApp()
html = app.fetch_content('https://example.com/novel/123')
app.parse_chapters(html)
app.show_reader()

# 漫画阅读
comic = ComicReader()
html = comic.fetch_content('https://example.com/comic/456')
comic.parse_chapters(html)
comic.show_comic()

关键点:

  1. requests+BeautifulSoup抓取和解析
  2. ui模块构建界面,WebView显示HTML内容
  3. 漫画需要处理图片下载和滚动视图
  4. 不同网站需要写不同的解析规则

建议:先搞定一个网站再扩展。

功能不错为什么没做一个列表,还需要手动改一下 url。。。

右上角有个菜单的符号,里面可以改 url。在考虑做 webview 的东西,但是可能比较恶心,pythonista 的 webview 感觉挺鸡肋的

ok 欢迎加入 py

回到顶部