PhantomJS可用於BeautifulSoup的替代方案嗎?如何用幻影鏈接
我試圖在Etsy上搜索並訪問所有鏈接的術語。在Python中,我知道如何做到這一點(使用BeautifulSoup),但今天我想看看我能否用PhantomJS做同樣的事情。我不是很遠。
此腳本應在Etsy上搜索「hello kitty」,並將所有產品返回 <a class="listing-thumb" href=...></a>
並在控制檯中打印它們。理想情況下,我會在稍後訪問它們並獲取我需要的信息。現在它只是凍結。有任何想法嗎?
var page = require('webpage').create();
var url = 'http://www.etsy.com/search?q=hello%20kitty';
page.open(url, function(status){
// list all the a.href links in the hello kitty etsy page
var link = page.evaluate(function() {
return document.querySelectorAll('a.listing-thumb');
});
for(var i = 0; i < link.length; i++){ console.log(link[i].href); }
phantom.exit();
});
我玩弄了使用CasperJS,這可能是更好的設計。
我建議查看[cheerio](https://github.com/MatthewMueller/cheerio)。它非常適合抓取網頁的任務,它的遍歷/操作API與jQuery非常相似。 – davidchambers