import json, re, urllib.request, urllib.parse

base_url = 'https://www.taixing.gov.cn/api-gateway/jpaas-publish-server/front/page/build/unit'
headers = {'Referer': 'https://www.taixing.gov.cn/zwgk/xxgk/zdly/sthj/index.html'}

# Try 1: Include queryData as JSON string parameter
query_data = {
    'webId': '569012ebb4b9462a9f6c633227a8ef5b',
    'pageId': 'NaLBGqC9X7yXPzYhbzhoI',
    'parseType': 'bulidstatic',
    'pageType': 'column',
    'tagId': '信息公开内容2',
    'tplSetId': 'a62207fcac654afba2ff9d55a7839094',
    'pageNo': 2,
    'rows': 15
}

params = {
    'queryData': json.dumps(query_data, ensure_ascii=False)
}
url = base_url + '?' + urllib.parse.urlencode(params)
print('URL:', url[:200])
req = urllib.request.Request(url, headers=headers)
try:
    resp = urllib.request.urlopen(req, timeout=15)
    data = json.loads(resp.read())
    html = data['data']['html']
    texts = re.findall(r'href="([^"]+)"[^>]*>([^<]+)</a>', html)
    print(f'Method 1: {len(texts)} links')
    for href, text in texts[:3]:
        print(f'  {href.split("/")[-1]} -> {text[:50]}')
    pagi = re.search(r'pageNo="(\d+)"', html)
    print(f'  pageNo={pagi.group(1) if pagi else "not found"}')
except Exception as e:
    print(f'Method 1 failed: {e}')

# Try 2: original params + pageNo separately  
params2 = {
    'parseType': 'bulidstatic',
    'webId': '569012ebb4b9462a9f6c633227a8ef5b',
    'tplSetId': 'a62207fcac654afba2ff9d55a7839094',
    'pageType': 'column',
    'tagId': '信息公开内容2',
    'editType': 'null',
    'pageId': 'NaLBGqC9X7yXPzYhbzhoI',
    'pageNo': '2',
    'rows': '15'
}
url2 = base_url + '?' + urllib.parse.urlencode(params2)
req2 = urllib.request.Request(url2, headers=headers)
try:
    resp2 = urllib.request.urlopen(req2, timeout=15)
    data2 = json.loads(resp2.read())
    html2 = data2['data']['html']
    texts2 = re.findall(r'href="([^"]+)"[^>]*>([^<]+)</a>', html2)
    print(f'\nMethod 2: {len(texts2)} links')
    for href, text in texts2[:3]:
        print(f'  {href.split("/")[-1]} -> {text[:50]}')
    pagi2 = re.search(r'pageNo="(\d+)"', html2)
    print(f'  pageNo={pagi2.group(1) if pagi2 else "not found"}')
except Exception as e:
    print(f'Method 2 failed: {e}')
