2014-09-18 3 views
-2

나는 이름, 주소, 페이지 (들)에서 전화 번호 및 리조트의 이메일 주소웹 : 문제 스크래핑 데이터베이스

http://www.exploreminnesota.com/places-to-stay/resorts/?keywords=&pageIndex=0&radius=0&mapTab=false&sortOrder=asc&sort=randomdaily&locationid=&startDate=false&class_id=7&lat=&lon=&city=&pageSize=20&type=reitlistings&attrFieldsOr=

I 당겨을 시도하고있어에서 온다 Ruby를 처음 접했을 때 몇 가지 예를 살펴 보았지만 적절한 솔루션을 찾기에는 너무 구체적 인 것처럼 보입니다.

이메일 주소 요소에 중점을 둡니다. '검사 된 요소'가 있고 CSS 경로를 사용했습니다 (# category-listings> li : nth-child (1)> div> div> ul> li : nth-child (2)> a)

루비 스크립트는이 데이터를 끌어 시도 :

require 'nokogiri' 
require 'open-uri' 

PAGE_URL = "http://www.exploreminnesota.com/places-to-stay/resorts/?keywords=&pageIndex=0&radius=0&mapTab=false&sortOrder=asc&sort=randomdaily&locationid=&startDate=false&class_id=7&lat=&lon=&city=&pageSize=20&type=reitlistings&attrFieldsOr=" 

page = Nokogiri::HTML(open(PAGE_URL)) 

site1 = page.css(' #category-listings li:nth-child(1) div div ul li:nth-child(2) a') 
puts site1 

출력 : R : I 이메일 주소를 원하는

href="mailto:**%7B%7Br._source.database_fields.email%7D%7D"** class="button gaTracker" title="**{{r._source.database_fields.email}}**" data-tracker-type="event" data-category="Email" data-label="{{r._source.location.split('/')[1]}}" data-action="{{url | analyticsAction}}">Email 

당신이 볼 수 있듯이을, 제목 통화로 표시됩니다. _source.database_fields.email

내가 브라우저 변호사와 같이 데이터에 액세스하는 방법을 잘 모르겠어요

href="mailto:[email protected]" class="button gaTracker" title="[email protected]" data-tracker-type="event" data-category="Email" data-label="gull-four-seasons-resort" data-action="Places to Stay">Email 

:이 요소를 검사하는 경우로

데이터가 표시되는지. 어떤 도움을 주시면 감사하겠습니다. 또한 HTML/CSS를 이해하고 데이터가 데이터베이스에서 페이지로 가져 오는 방법을 이해하는 데 도움이됩니다.

감사합니다.

+0

처음 페이지가로드 될 때까지 페이지에서 스크래핑 할 데이터가 보이지 않는 것처럼 보입니다. 즉, 데이터를 가져올 수있는 기회가 오기 전에 Nokogiri로 페이지를 구문 분석하고있는 것 같습니다. 다음은 비슷한 문제가있는 질문자입니다. http://stackoverflow.com/q/4341387/3367343 – thohl

+0

7stud. 너 진짜 스터드 야! 아마 사람들이 너를 존중할거야. 또한 통찰력을 가져 주셔서 감사합니다.저는 게시 조건을 읽었으며 그러한 문제에 대해 새로운 사람들을 도울 정보에 입각 한 질문을 만들려고 노력했습니다. 감사합니다, 진심으로 감사드립니다. – user8264

+0

여러분을 환영합니다! – thohl

답변

1

다음 코드는 exploreminnesota.com에 에 대해서만의 GET 요청을 보내 원하는 JSON 데이터 즉, 더 이상 Nokogiri가 필요하지 않게합니다. 그런 다음 응답 (임시 파일)을 Ruby JSON 객체로 변환하고 객체를 터미널로 출력합니다. url에 할당 된 URL에

require "open-uri" 
require "json" 

url = "http://www.exploreminnesota.com/getJsonData.ashx?id=61&keywords=&pageIndex=0&radius=0&mapTab=false&sortOrder=asc&sort=randomdaily&locationid=&startDate=false&class_id=7&lat=&lon=&city=&pageSize=20&type=reitlistings&attrFieldsOr=" 

response_file = open(url) # Make HTTP request and save as temp file 
response_json = JSON.parse(response_file.read) # Convert response to JSON 

puts JSON.pretty_generate(response_json) 

공지 사항 getJsonData.ashx -이 URL이 아니라 HTML보다 JSON 데이터를 검색합니다.

크롬 인스펙터를 사용하여 url을 찾았습니다. 관리자에서 네트워크 탭으로 이동하여 '캐시 사용 중지'를 선택하고 페이지를 새로 고치고 'XHR'요청 유형에 대한 결과를 필터링합니다. 여기에는 초기 페이지로드 이후에 브라우저가 작성한 XHR 요청 및 응답 목록이 표시됩니다 (6 점). 이제 '도시'라는 단어에 대한 결과를 필터링하면 관심있는 네트워크 요청이 하나씩 표시됩니다. 그것을 클릭 한 다음 "미리보기"탭을 클릭하십시오 - 이제 개체를 확장 및 축소하여 속성에서 JSON 응답을 탐색 할 수 있습니다.

당신이 위에 내 예에 다음 행을 추가하는 경우 :

puts response_json["hits"]["hits"][0]["_source"]["database_fields"]["email"] 

는 첫 번째 리조트의 이메일 주소를 인쇄합니다.