108 lines
3.0 KiB
TypeScript
108 lines
3.0 KiB
TypeScript
import { chromium } from 'playwright';
|
|||
|
|
|
||
|
|
async function inspectPage() {
|
||
|
|
console.log('启动浏览器...');
|
||
|
|
const browser = await chromium.launch({ headless: true });
|
||
|
|
const page = await browser.newPage();
|
||
|
|
|
||
|
|
try {
|
||
|
|
console.log('导航到页面...');
|
||
|
|
await page.goto('https://i.jzj9999.com/quoteh5/', {
|
||
|
|
waitUntil: 'networkidle',
|
||
|
|
timeout: 30000
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('\n页面加载成功!');
|
||
|
|
console.log('页面标题:', await page.title());
|
||
|
|
console.log('页面URL:', page.url());
|
||
|
|
|
||
|
|
// 获取页面的 HTML 结构
|
||
|
|
console.log('\n查找所有包含"银"字的元素...');
|
||
|
|
const silverElements = await page.evaluate(() => {
|
||
|
|
const elements = Array.from(document.querySelectorAll('*'));
|
||
|
|
const results: string[] = [];
|
||
|
|
|
||
|
|
elements.forEach(el => {
|
||
|
|
const text = el.textContent?.trim() || '';
|
||
|
|
if (text.includes('银') && text.length < 50) {
|
||
|
|
const tagName = el.tagName.toLowerCase();
|
||
|
|
const className = el.className || '';
|
||
|
|
results.push(`${tagName}.${className}: "${text}"`);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
return results.slice(0, 20); // 只返回前20个
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('找到的元素:');
|
||
|
|
silverElements.forEach((el, i) => {
|
||
|
|
console.log(` ${i + 1}. ${el}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 查找所有可能的价格容器
|
||
|
|
console.log('\n查找价格容器结构...');
|
||
|
|
const structure = await page.evaluate(() => {
|
||
|
|
// 查找所有可能的容器
|
||
|
|
const containers = [
|
||
|
|
'div.e19',
|
||
|
|
'div.generic',
|
||
|
|
'[class*="price"]',
|
||
|
|
'[class*="item"]',
|
||
|
|
'[class*="row"]'
|
||
|
|
];
|
||
|
|
|
||
|
|
const results: any = {};
|
||
|
|
containers.forEach(selector => {
|
||
|
|
const elements = document.querySelectorAll(selector);
|
||
|
|
results[selector] = elements.length;
|
||
|
|
});
|
||
|
|
|
||
|
|
return results;
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('容器统计:');
|
||
|
|
Object.entries(structure).forEach(([selector, count]) => {
|
||
|
|
console.log(` ${selector}: ${count} 个`);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 尝试获取所有商品名称
|
||
|
|
console.log('\n尝试获取商品列表...');
|
||
|
|
const products = await page.evaluate(() => {
|
||
|
|
const results: string[] = [];
|
||
|
|
|
||
|
|
// 尝试多种选择器
|
||
|
|
const selectors = [
|
||
|
|
'div.e19 > div.generic',
|
||
|
|
'div.generic',
|
||
|
|
'[class*="item"]',
|
||
|
|
'[class*="product"]'
|
||
|
|
];
|
||
|
|
|
||
|
|
for (const selector of selectors) {
|
||
|
|
const elements = document.querySelectorAll(selector);
|
||
|
|
if (elements.length > 0) {
|
||
|
|
results.push(`\n使用选择器: ${selector} (找到 ${elements.length} 个)`);
|
||
|
|
|
||
|
|
Array.from(elements).slice(0, 5).forEach((el, i) => {
|
||
|
|
const text = el.textContent?.trim().substring(0, 100) || '';
|
||
|
|
results.push(` ${i + 1}. ${text}`);
|
||
|
|
});
|
||
|
|
|
||
|
|
break; // 找到就停止
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return results;
|
||
|
|
});
|
||
|
|
|
||
|
|
products.forEach(line => console.log(line));
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('错误:', error);
|
||
|
|
} finally {
|
||
|
|
await browser.close();
|
||
|
|
console.log('\n浏览器已关闭');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
inspectPage();
|