问题是内容是异步加载的,首先加载一个几乎是空的页面,然后加载您要查找的内容。
只需 console.log 请求的正文,您就会看到如下内容:
<!DOCTYPE html>
<html ng-app='HNSearch'>
<head ng-controller='HeadCtrl'>
...
links and meta
...
</head>
<body>
<div id='main' ng-cloak role='main' ui-view>
! NO CONTENT HERE ! IT WILL BE LOADED AFTER
</div>
<script src="https://d3nb9u6x572n0.cloudfront.net/assets/application-70dfa2f5ecb75bc8dfaa8729257bcbf1.js"></script>
</body>
</html>
如果你用谷歌浏览器检查网页,你会看到这个链接被调用了:
https://uj5wyc0l7x-dsn.algolia.net/1/indexes/Item_production/query?x-algolia-api-key=8ece23f8eb07cd25d40262a1764599b1&x-algolia-application-id=UJ5WYC0L7X&x-algolia-agent=Algolia%20for%20AngularJS%203.7.5
所以最后我找到了这个可以帮助你的解决方案:
request.post({
url:'https://uj5wyc0l7x-dsn.algolia.net/1/indexes/Item_production/query?x-algolia-api-key=8ece23f8eb07cd25d40262a1764599b1&x-algolia-application-id=UJ5WYC0L7X&x-algolia-agent=Algolia%20for%20AngularJS%203.7.5',
body:'{"params":"query=apple&hitsPerPage=20&minWordSizefor1Typo=5&minWordSizefor2Typos=9&advancedSyntax=true&ignorePlurals=false&tagFilters=%5B%22story%22%5D&numericFilters=%5B%5D&page=0&queryType=prefixLast&typoTolerance=true&restrictSearchableAttributes=%5B%5D"}',
gzip:true,
headers:{
accept:'application/json',
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"es-ES,es;q=0.8",
"Cache-Control":"no-cache",
Connection:"keep-alive",
"Content-Length":258,
"content-type":"application/x-www-form-urlencoded",
Host:"uj5wyc0l7x-dsn.algolia.net",
Origin:"https://hn.algolia.com",
Pragma:"no-cache",
Referer:"https://hn.algolia.com/?query=apple&sort=byPopularity&prefix&page=0&dateRange=all&type=story",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
}
},
function (err,res,body) {
console.log(body);
});
现在正文是一个包含所有数据的巨大 JSON 文件。
我希望这对你有帮助。