我似乎無法使用Artoo和Puppeteer。我試圖通過npm install artoo-js
使用它,它沒有工作。我也嘗試使用木偶命令page.injectFile(filePath)
注入構建路徑dist。沒有運氣。任何人都能夠成功地暗示這兩個庫?如果是的話,會喜歡artoo的注入代碼片段。使用Artoo js與Google Puppeteer進行網絡抓取
1
A
回答
3
我只是想爲木偶another answer,我想我可以嘗試Artoo了,所以在這裏你去:)
(第0步:安裝Yarn,如果你沒有的話)
yarn init
yarn add puppeteer
# Download latest artoo script, not as a yarn dependency here because it won't be by the Node JS runtime
wget https://medialab.github.io/artoo/public/dist/artoo-latest.min.js
在index.js
保存此:
const puppeteer = require('puppeteer');
(async() => {
const url = 'https://news.ycombinator.com/';
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Go to URL and wait for page to load
await page.goto(url, {waitUntil: 'networkidle'});
// Inject Artoo into page's JS context
await page.injectFile('artoo-latest.min.js');
// Sleeping 2s to let Artoo initialize (I don't have a more elegant solution right now)
await new Promise(res => setTimeout(res, 2000))
// Use Artoo from page's JS context
const result = await page.evaluate(() => {
return artoo.scrape('td.title:nth-child(3)', {
title: {sel: 'a'},
url: {sel: 'a', attr: 'href'}
});
});
console.log(`Result has ${result.length} items, first one is:`, result[0]);
browser.close();
})();
結果:
$ node index.js
Result has 30 items, first one is: { title: 'Headless mode in Firefoxdeveloper.mozilla.org',
url: 'https://developer.mozilla.org/en-US/Firefox/Headless_mode' }
這是太可笑錯過:現在HackerNews頂部文章是關於Firefox的無頭...
相關問題
- 1. 如何在Google上進行網絡抓取
- 2. Java利用多個IP進行網絡抓取,使用線程
- 3. 使用Python進行網頁抓取
- 4. 使用Java Swing進行網頁抓取
- 5. 網絡抓取cnbc.com
- 6. 用R和選擇器小工具進行網絡抓取
- 7. Node.js網絡抓取
- 8. 如何從(javascript?)網站進行網絡抓取?
- 9. 使用Python/Pexpect的抓取網絡
- 10. 使用R網絡抓取數據
- 11. 使用Angular js抓取網站
- 12. 網絡抓取登錄
- 13. 網絡抓取計劃
- 14. 優化網絡抓取
- 15. 網絡抓取鏈接表
- 16. 網絡抓取錯誤
- 17. 使用網絡爬取器抓取網頁數據
- 18. 使用BeautifulSoup進行網頁抓取:正在獲取fundsupermart數據
- 19. 使用python進行網頁抓取以提取數據
- 20. Python 3,網絡抓取和Javascript [Oh My]
- 21. Node.js使用chrome-remote-interface進行抓取
- 22. 網絡抓取,獲取空列表
- 23. 網絡抓取時內存泄漏
- 24. 使用tryCatch與網頁抓取
- 25. R網絡抓取,從網絡應用程序下載數據
- 26. scraperjs:網絡抓取代碼不工作
- 27. 網絡抓取Google - 獲得不同的結果
- 28. 網絡抓取密碼保護網站使用R
- 29. 如何從有加載表的網站上進行網絡抓取?
- 30. 技術爲網絡抓取需要?
我沒有你的問題準確的答案。但是我寫了一篇關於[用Puppeteer和Chrome無頭的網頁報廢](https://medium.com/@e_mad_ehsan/getting-started-with-puppeteer-and-chrome-headless-for-web-scrapping-6bf5979dee3e)。可能有幫助。 – eMAD