const puppeteer = require("puppeteer");
const url = "https://music.163.com/";
const cheerio = require("cheerio")
async function show () {
const browser = await puppeteer.launch({
defaultViewport: null, //可视区域最大化
});
const page = await browser.newPage();
await page.goto(url)
await page.waitForSelector("iframe");
const elementHandle = await page.$('iframe');
const frame = await elementHandle.contentFrame();
const content = await frame.content(frame);
const $ = cheerio.load(content,{
decodeEntities:false
})
var items = $(".n-rcmd ul.f-cb li")
var arr = [];
items.each((index,ele)=>{
var title = $(ele).find(".dec a").html()
var img = $(ele).find(".u-cover img").attr("src")
arr.push({
title,
img
})
})
console.log(arr)
}
show()
# .contentFrame() 获取iframe中的html结构。
const elementHandle = await page.$('iframe');
const frame = await elementHandle.contentFrame();
1-1 在iframe的input框中输入
const puppeteer = require("puppeteer");
const url = "https://login.anjuke.com/login/form";
const cheerio = require("cheerio")
async function show () {
const browser = await puppeteer.launch({
headless:false,
defaultViewport: null, //可视区域最大化
});
const page = await browser.newPage();
await page.goto(url)
await page.waitForSelector("#iframeLoginIfm");
const elementHandle = await page.$('#iframeLoginIfm');
const frame = await elementHandle.contentFrame();
const tel = await frame.$("#phoneIpt");
await tel.type("189")
}
show()
1-2 frames().find()获取iframe
const puppeteer = require("puppeteer");
const url = "https://login.anjuke.com/login/form";
async function show () {
const browser = await puppeteer.launch({
headless:false,
defaultViewport: null, //可视区域最大化
});
const page = await browser.newPage();
await page.goto(url)
/* 切换iframe */
var target = "https://login.anjuke.com/login/iframeform"
const frame = await page.frames().find(frame=>frame.url().includes(target));
var input = await frame.$("#phoneIpt");
await input.type("189")
}
show()