저는 Nokogiri를 사용하고 있습니다. 현재 일부 페이지의 코드를 포함하는 변수가 있습니다 doc = Nokogiri::HTML(open(page))
.. 코드에는 ajax 호출 스크립트가 있습니다.
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$("#menu").kendoMenu();
$('.menu_item').on('click', function (e){
$.ajax({
url: '/movie/101299-the-hunger-games-catching-fire/images?kind=backdrop&language=' + $(this).attr('alt') + '&translate=false',
cache: false
}).done(function(response) {
$('#image_panel').html(response);
});
});
$.ajax({
url: '/movie/101299-the-hunger-games-catching-fire/images?kind=backdrop&language=&translate=false', //goal
cache: false
}).done(function(response) {
$('#image_panel').html(response);
});
});
</script>
두 번째 요청 URL을 가져 와서 변수에 배치하는 방법이 있습니다.이 URL로 이동해야합니다. 불행히도 나는 그것에 대해 뭔가를 찾지 못했습니다. 어쩌면 phantomjs가 나를 도울 수 있습니까?
스크립트 요소를 수동으로 구문 분석 할 것이라고 생각합니다. Nokogiri를 사용하여 스크립트 요소의 텍스트를 가져 오면됩니다. 그런 다음 정규 표현식을 사용하여 마지막 URL을 찾습니다.
스크립트가 페이지의 첫 번째라고 가정하면 다음을 수행 할 수 있습니다.
url = doc.at_css('script').text.scan(/url: '(.*)'/).last.first
다음은 스크립트를 분할하여 각 단계에 대한 설명을 제공합니다.
# Get the text of the script element
# Note that this assumes it is the first script element (you may need to be more specific)
script = doc.at_css('script').text
# Find all urls in the script
urls = script.scan(/url: '(.*)'/)
# Of the urls found, take the last one
url = urls.last
# url is actually an array of length 1, since we used a matching group in the regex
# Take the first element of the array to get the url as a string
url = url.first
#=> "/movie/101299-the-hunger-games-catching-fire/images?kind=backdrop&language=&translate=false"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다