크롤링하는 HTML 페이지에서 JavaScript 개체를 구문 분석하는 방법은 무엇입니까? (How to parse a JavaScript object from a HTML page I crawl?)


문제 설명

크롤링하는 HTML 페이지에서 JavaScript 개체를 구문 분석하는 방법은 무엇입니까? (How to parse a JavaScript object from a HTML page I crawl?)

음식 레시피 페이지의 색인을 생성하려고 하는데 실제 레시피는 페이지의 JavaScript 내에 객체로 저장됩니다.

예제 URL: http://www.dagbladet.no/mat/oppskrift/bakt‑potet‑med‑romme ‑og‑blamuggostdressing

브라우저에서 개발자 도구를 열고 다음을 입력하면:

console.dir(food.recipeItem.title)

제목을 다시 얻습니다.

6
"Bakt potet med rømme‑ og blåmuggostdressing"

전체 멋지고 멋지고 내가 필요한 것. 그러나 어떻게 그 스크립트를 파악하고 Node.js 애플리케이션 내에서 구문 분석할 수 있습니까? Cheerio가 대본을 찾는 데 도움이 될 수 있지만 그 이상은 하지 않습니까? 아니면 그럴까요? 어떻게 해야할지 모르겠지만, 가장 계산 효율적인 답변이 아닙니다. 또는 가장 견고합니다.


참조 솔루션

방법 1:

It's pretty easy, all you have to do is parse the returned HTML. If you inspect the returned HTML (view‑source:http://www.dagbladet.no/mat/oppskrift/bakt‑potet‑med‑romme‑og‑blamuggostdressing), you will find a script tag which contains all information you need in several javascript variables. These variables holds JSON data. Since the script is hardcoded directly into the HTML document, and not obtained by XHR or similar, parsing the HTML is the only way of doing this.

So basically you have these 3 steps:

1. send HTTP GET request to the link above

2. parse the HTML string to extract the script tag by using some library (check this link to decide which library to use).

3. parse the javascript string (extracted script from step 2) to extract JSON data. Check UglifyJS library for Node.js

(by Espen KlemBoy)

참조 문서

  1. How to parse a JavaScript object from a HTML page I crawl? (CC BY‑SA 2.5/3.0/4.0)

#web-crawler #Parsing #javascript #html-parsing #node.js






관련 질문

UnicodeError: URL에 ASCII가 아닌 문자가 포함되어 있습니다(Python 2.7). (UnicodeError: URL contains non-ASCII characters (Python 2.7))

크롤링 출력 - 두 변수 연결 (Crawling output - connecting two variables)

Python2.7에서 효과적인 크롤러를 만드는 방법 (How to make an effective crawler in Python2.7)

이 텍스트가 다른 기사의 일부임을 Google에 알리는 방법 (How to tell google this text is part of another article)

크롤링하는 HTML 페이지에서 JavaScript 개체를 구문 분석하는 방법은 무엇입니까? (How to parse a JavaScript object from a HTML page I crawl?)

데이터 크롤링 또는 API 사용 (Crawling data or using API)

파이썬을 사용하여 웹사이트에서 내부 링크만 크롤링 (Crawl only internal links from a website using python)

받은 응답에서 HTML 코드를 긁는 방법은 무엇입니까? (How to scrape the html code from the response received?)

PHP를 사용하여 웹 사이트에서 클래스 이름 스크래핑 (Scraping class name on a website using php)

Scrapy Spider를 사용하는 Craigslist Scraper가 기능을 수행하지 않음 (Craigslist Scraper using Scrapy Spider not performing functions)

BeautifulSoup: 이 링크에서 모든 기사 링크를 가져오는 방법은 무엇입니까? (BeautifulSoup: how to get all article links from this link?)

나는 클라이언트입니다. 선택적으로 http 응답에서 헤더를 제거할 수 있습니까? (I'm client. Can I remove header from http response optionally?)







코멘트