#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""v0.1 源结构侦察：抓取样页，找标题/日期/链接的真实 DOM 规律。"""
import re
import sys

import requests
from bs4 import BeautifulSoup

UA = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 '
      '(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36')
S = requests.Session()
S.headers.update({'User-Agent': UA, 'Accept-Language': 'zh-CN,zh;q=0.9'})

TARGETS = [
    ('ggzy-中标结果', 'https://www.ggzy.gov.cn/deal/dealList.html?HEADER_DEAL_TYPE=01'),
    ('诺唯赞-新闻', 'https://www.vazyme.com/news/1/'),
    ('全式金-新闻', 'https://www.transgen.com/news/list_1.html'),
    ('翌圣-新闻分类', 'https://www.yeasen.com/news/'),
    ('罗氏-新闻列表', 'https://diagnostics.roche.com/global/en/news-listing.html'),
    ('赛默飞中国-新闻', 'https://www.thermofisher.cn/cn/zh/home/about-us/news-gallery.html'),
    ('菲鹏-列表', 'https://www.fapon.com/f/list-411.html'),
]

for name, url in TARGETS:
    print('=' * 78)
    print('■', name, url)
    try:
        r = S.get(url, timeout=30)
        print('  状态:', r.status_code, '｜ 字节:', len(r.content), '｜ 编码:', r.encoding)
        r.encoding = r.apparent_encoding or r.encoding
        soup = BeautifulSoup(r.text, 'lxml')
        title = soup.title.get_text(strip=True) if soup.title else ''
        print('  标题:', title[:70])
        # 找候选条目：含链接且文本长度适中
        cands = []
        for a in soup.find_all('a', href=True):
            t = re.sub(r'\s+', ' ', a.get_text(' ', strip=True))
            if 8 <= len(t) <= 90 and not t.startswith(('首页', '关于', '更多')):
                cands.append((t, a['href']))
        print('  候选链接数:', len(cands))
        seen = set()
        shown = 0
        for t, h in cands:
            if t in seen:
                continue
            seen.add(t)
            print('    · %-58s %s' % (t[:58], h[:60]))
            shown += 1
            if shown >= 6:
                break
        # 日期规律
        dates = re.findall(r'(20\d{2}[-/年]\s?\d{1,2}[-/月]\s?\d{1,2})', r.text)
        print('  页面内日期样例:', dates[:5])
    except Exception as e:
        print('  ❌ 失败:', str(e)[:120])
    print()
