diff --git a/sehuatang/shehuatang.py b/sehuatang/shehuatang.py index 7165bd0..19aea58 100644 --- a/sehuatang/shehuatang.py +++ b/sehuatang/shehuatang.py @@ -10,7 +10,7 @@ from webdriver_manager.chrome import ChromeDriverManager from bs4 import BeautifulSoup from reportlab.lib.pagesizes import letter from reportlab.lib import colors -from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image +from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, PageBreak from reportlab.lib.styles import getSampleStyleSheet from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfbase import pdfmetrics @@ -20,6 +20,7 @@ import re from PyPDF2 import PdfReader, PdfWriter +# download_image 函数保持不变 def download_image(url): """下载大于100KB的图片并返回临时文件路径,仅支持jpg、jpeg和png格式""" try: @@ -28,11 +29,11 @@ def download_image(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', - 'Referer': 'https://tu.a7nz4.us', # 防止403 + 'Referer': 'https://tu.a7nz4.us', } response = requests.get(url, headers=headers) - response.raise_for_status() # 确保请求成功 + response.raise_for_status() image = BytesIO(response.content) return image except requests.exceptions.RequestException as e: @@ -42,43 +43,39 @@ def download_image(url): def fetch_and_create_pdf(url): """根据给定URL抓取页面并生成PDF""" - # 配置Selenium以无头模式运行 + # 配置Selenium options = Options() options.headless = True options.add_argument('--disable-gpu') options.add_argument('--no-sandbox') - - # 使用webdriver-manager自动下载ChromeDriver driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) # 获取目标页面 driver.get(url) time.sleep(5) - # 处理“满18岁,请点此进入”按钮 + # 处理年龄验证按钮 try: enter_button = driver.find_element(By.XPATH, '//a[contains(text(), "满18岁,请点此进入")]') enter_button.click() print("点击了满18岁按钮") - time.sleep(5) # 等待内容加载 + time.sleep(5) except Exception as e: print("未找到满18岁按钮,跳过此步骤", e) - # 使用BeautifulSoup解析页面 + # 解析页面 html = driver.page_source soup = BeautifulSoup(html, 'html.parser', from_encoding='utf-8') - - # 定位到帖子列表 posts = soup.find_all('tbody', {'id': lambda x: x and x.startswith('normalthread')}) # 获取今天的日期 today = datetime.now().strftime('%Y-%m-%d') # 注册中文字体 - pdfmetrics.registerFont(TTFont('SimHei', 'fonts/simhei.ttf')) # 请确保字体文件路径正确 + pdfmetrics.registerFont(TTFont('SimHei', 'fonts/simhei.ttf')) styles = getSampleStyleSheet() - # 设置标题和正文样式 + # 设置样式 title_style = styles['Heading1'] title_style.fontName = 'SimHei' title_style.fontSize = 14 @@ -90,65 +87,43 @@ def fetch_and_create_pdf(url): content = [] - # 过滤出当天的帖子 + # 过滤当天帖子并倒序 today_posts = [] for post in posts: post_time_span = post.find('span', {'class': 'xi1'}) - if post_time_span: # 判断是否为当天帖子 + if post_time_span: today_posts.append(post) + today_posts = today_posts[::-1] # 倒序处理 # 设置PDF pdf_filename = f"JAV-{today}-{len(today_posts)}.pdf" doc = SimpleDocTemplate(pdf_filename, pagesize=letter) - # 遍历当天的帖子并提取信息 + # 遍历帖子 for post in today_posts: - # 查找帖子标题 title = post.find('a', {'class': 's xst'}) if title: post_title = title.get_text() post_url = title.get('href') print(post_title) - # 获取帖子页面 + + # 获取帖子内容 post_page_url = 'https://www.sehuatang.net/' + post_url driver.get(post_page_url) time.sleep(3) - # 获取帖子页面内容 post_html = driver.page_source post_soup = BeautifulSoup(post_html, 'html.parser', from_encoding='utf-8') - - # 提取