69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""测试分页URL格式"""
|
|
from playwright.sync_api import sync_playwright
|
|
from bs4 import BeautifulSoup
|
|
import re
|
|
|
|
def test_pagination(menu_id=7):
|
|
with sync_playwright() as p:
|
|
browser = p.chromium.launch(headless=True)
|
|
page = browser.new_page(viewport={'width': 1920, 'height': 1080})
|
|
|
|
# 先打开第1页
|
|
url1 = f"https://niaodaifu.cn/health-information?menu_id={menu_id}"
|
|
print(f"[第1页] GET {url1}")
|
|
page.goto(url1, wait_until='networkidle', timeout=30000)
|
|
page.wait_for_timeout(3000)
|
|
soup1 = BeautifulSoup(page.content(), 'html.parser')
|
|
medias1 = soup1.find_all('div', class_='media')
|
|
ids1 = []
|
|
for m in medias1:
|
|
a = m.find('a', class_='media-heading')
|
|
if a:
|
|
href = a.get('href', '')
|
|
match = re.search(r'article_id=(\d+)', href)
|
|
if match:
|
|
ids1.append(match.group(1))
|
|
print(f" 第1页 article_ids: {ids1}")
|
|
|
|
# 尝试几种分页URL格式
|
|
formats = [
|
|
f"https://niaodaifu.cn/health-information?menu_id={menu_id}&page=2",
|
|
f"https://niaodaifu.cn/health-information?menu_id={menu_id}&pageNo=2",
|
|
f"https://niaodaifu.cn/health-information?menu_id={menu_id}&pageIndex=2",
|
|
]
|
|
|
|
for url2 in formats:
|
|
print(f"\n[第2页] GET {url2}")
|
|
page.goto(url2, wait_until='networkidle', timeout=30000)
|
|
page.wait_for_timeout(3000)
|
|
soup2 = BeautifulSoup(page.content(), 'html.parser')
|
|
medias2 = soup2.find_all('div', class_='media')
|
|
ids2 = []
|
|
for m in medias2:
|
|
a = m.find('a', class_='media-heading')
|
|
if a:
|
|
href = a.get('href', '')
|
|
match = re.search(r'article_id=(\d+)', href)
|
|
if match:
|
|
ids2.append(match.group(1))
|
|
print(f" 第2页 article_ids: {ids2}")
|
|
if ids2 != ids1:
|
|
print(f" *** 分页有效!与第1页不同 ***")
|
|
break
|
|
else:
|
|
print(f" !!! 分页无效,与第1页相同 !!!")
|
|
|
|
# 查看分页组件HTML
|
|
print("\n\n[分页组件HTML]")
|
|
pager = soup1.find(class_=re.compile('el-pagination'))
|
|
if pager:
|
|
print(pager.prettify()[:1000])
|
|
else:
|
|
print("未找到 el-pagination")
|
|
|
|
browser.close()
|
|
|
|
if __name__ == '__main__':
|
|
test_pagination()
|