i have the following python script to retrieve the weixin article contents from the url…
- User
i have the following python script to retrieve the weixin article contents from the url found in CSV.
import requests from bs4 import BeautifulSoup import base64 from urllib.parse import urljoin import csv import re import os
def get_base64_image(url, headers): try: resp = requests.get(url, headers=headers, timeout=10) resp.raise_for_status() b64 = base64.b64encode(resp.content).decode() return b64 except Exception as e: print(f"圖片下載失敗 {url}: {e}") return None
def parse_input_date(timestr): """ 將日/月/年 時:分:秒格式轉換成 年-月-日 時:分:秒 timestr範例: '24/06/2024 14:30:59' """ if not timestr: return ""
# 使用正規表達式來解析日期時間 match = re.match(r'(\d{1,2})/(\d{1,2})/(\d{4})\s+(\d{1,2}):(\d{2})(?::(\d{2}))?', timestr) if not match: return "" day, month, year, hour, minute, second = match.groups() day = day.zfill(2) month = month.zfill(2) hour = hour.zfill(2) minute = minute.zfill(2) second = second if second else "00" second = second.zfill(2) return f"{year}-{month}-{day} {hour}:{minute}:{second}"def get_article_info(url, held_date=""): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/90.0.4430.212 Safari/537.36', 'Referer': 'https://mp.weixin.qq.com/' }
try: resp = requests.get(url, headers=headers, timeout=10) resp.encoding = 'utf-8' if resp.status_code != 200: print(f"無法取得頁面:狀態碼 {resp.status_code} URL:{url}") return None soup = BeautifulSoup(resp.text, 'html.parser') title_tag = soup.find('h1', id='activity-name') title = title_tag.get_text(strip=True) if title_tag else "" if not title: print(f"找不到標題,URL: {url}") return None title_base64 = base64.b64encode(title.encode('utf-8')).decode() # 這裡改成用傳入的 held_date,不再從頁面找 held_date_val = held_date cover_base64 = "" cover_div = soup.find('div', id='js_row_immersive_cover_img') if cover_div: cover_img = cover_div.find('img') if cover_img and cover_img.get('src'): cover_url = cover_img['src'] if cover_url.startswith('//'): cover_url = 'https:' + cover_url elif cover_url.startswith('/'): cover_url = urljoin(url, cover_url) b64 = get_base64_image(cover_url, headers) if b64: ext = cover_url.split('.')[-1].lower() if ext not in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']: ext = 'jpeg' cover_base64 = f"data:image/{ext};base64,{b64}" content_div = soup.find('div', id='js_content') if not content_div: print(f"找不到文章內容節點,URL: {url}") return None content_soup = BeautifulSoup(str(content_div), 'html.parser') imgs = content_soup.find_all('img') for img in imgs: src = img.get('data-src') or img.get('src') if not src: continue if src.startswith('//'): src = 'https:' + src elif src.startswith('/'): src = urljoin(url, src) b64 = get_base64_image(src, headers) if b64: ext = src.split('.')[-1].lower() if ext not in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']: ext = 'jpeg' data_uri = f"data:image/{ext};base64,{b64}" img['src'] = data_uri if 'data-src' in img.attrs: del img.attrs['data-src'] full_html = f"""<html> - Assistant
- User
The held_date should be found in the element with id publish_date, the format like 2025年07月12日 17:25:38, please retrieve the date from there and convert with format Y-m-d H:i:s
- Assistant