मैं चीयरियो और कठपुतली मॉड्यूल का उपयोग करके अपने एचटीएमएल प्रतिक्रिया से सिर्फ ईमेल (myemail@hotmail.com) को अंगूर करने की कोशिश कर रहा हूं। लेकिन मुझे अलग-अलग चीजें मिल रही हैं जिन्हें मुझे उन सभी का उपयोग करने की आवश्यकता नहीं है। इसे कक्षा p2 में td/tr में रखा गया है। में पैरामीटर के रूप में tr डालते समय
इस तरह मेरा कोड दिखता है:
const puppeteer = require('puppeteer');
const $ = require('cheerio');
const url = 'https://mywebsite.com';
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(function(html) {
$('tr', html).each(function() {
// putting all the result into the list
console.log($(this).text());
});
})
.catch(function(err) {
//handle error
});
मुझे यह आउटपुट मिल रहा है:
मोबाइल पोस्ट बॉक्स सर्किट
myemail@hotmail.com
ई-मेल myemail@hotmail.com प्रबंधक सचिवमुझे बस myemail@hotmail.com की जरूरत है
यह मेरी एचटीएमएल तालिका है:
</td>
</tr>
<tr>
<td class="p1">E-mail</td>
<td class="p2">
<span style="float: none; word-wrap: break-word;"> <a href="mailto:myEmal@hotmail.com"> myEmal@hotmail.com
<div style="padding-right: 2px; background-position: -115px -434px; height: 14px !important; float: right" class="ico"></div>
</a>
</span>
</td>
2 जवाब
अपने एचटीएमएल को ध्यान में रखते हुए यह सबसे आसान तरीका होगा:
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
ध्यान दें कि स्क्रैप करने के बाद आपको ब्राउज़र को बंद करना होगा:
let _browser;
puppeteer
.launch()
.then(function(browser) {
_browser = browser; // <-- memorize browser reference
return _browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(function(html) {
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
})
.then(function(){
_browser.close() // <-- use it to close the browser
})
यदि आप नोड 8+ चला रहे हैं, तो ऐसी स्क्रिप्ट के लिए async/प्रतीक्षा का उपयोग करना बेहतर है।
उस वर्ग के टीडी के भीतर सामग्री प्राप्त करने का प्रयास करें।
console.log($(this).find('td.p2').text());