我想用啦啦队来刮胡子.但是我有一个小问题.我在客户端得到的所有href值都以"/url?q=".例如,像这样:
'/url?q=https://www.nimh.nih.gov/health/topics/auti… pkCZkQFnoECAYQAg&usg=AOvVaw1E4L1bLVm9OdBSFMkjJftQ'
Google搜索的元素是:
<a jsname="UWckNb" href="https://www.nimh.nih.gov/health/topics/autism-spectrum-disorders-asd"...
它不包含"/url?q="
个."/url?q="
从何而来?
app.get('/scrape', (req, res) => {
request('https://www.google.com/search?q=asd', (error, response, html) => {
if (response.statusCode == 200) {
const $ = cheerio.load(html);
const results = [];
const links = $('a');
links.each((index, link) => {
const href = $(link).prop('href');
const h3 = $(link).find('h3');
if (h3.length > 0) {
const textContent = h3.text().trim();
results.push({ href, textContent });
}
});
const responseData = {
links: results,
total: results.length
};
res.json(responseData);
} else {
console.error('Unexpected status code:', response.statusCode);
res.status(500).send('Unexpected status code.');
}
});
});
我知道我可以这样解决它:
const actualUrl = decodeURIComponent(href.split('/url?q=')[1].split('&')[0]);
但我想知道这"/url?q="
在哪里,我做错了什么?