2013-08-10 1 views
2

나는 mechanicalize를 사용하는 ruby ​​스크립트를 만들었습니다. google.com으로 이동하여 로그인하고 고양이에 대한 이미지 검색을 수행합니다. 다음 페이지에서 결과 링크 중 하나를 선택하고 이미지를 저장하고 싶습니다.루비에서 mechanicalize로 링크를 선택하는 것

내 문제는 모든 결과에 대한 링크가 빈 문자열로 표시되므로 지정하고 클릭하는 방법이 확실하지 않습니다.

여기에 pp 페이지의 출력이므로, 내가 말하는 링크를 볼 수 있습니다. 첫 번째 링크는 추천 링크이며 "지난 24 시간"이라는 제목이 있기 때문에 클릭 할 수 있지만 두 번째 링크는 클릭 할 수없는 검색의 실제 결과입니다.

page.links.each do |link| 
puts link.text. 
end 

페이지의 링크를 표시합니다

#<Mechanize::Page::Link 
    "Past 24 hours" 
    "https://stackoverflow.com/search?q=cats&hl=en&gbv=1&ie=UTF8&tbm=isch&source=lnt&tbs=qdr:d&sa=X&ei=T8kDUu7aB4f8iwKZx4HoBg&ved=0CCQQpwUoAQ"> 

#<Mechanize::Page::Link 
"" 
"http://www.google.com/imgres?imgurl=http://jasonlefkowitz.net/wp-content/uploads/2013/07/Cute-Cats-cats-33440930-1280-800.jpg&imgrefurl=http://jasonlefkowitz.net/2013/07/slideshow-20-cats-that-suck-at-reducing-tensions-in-the-israeli-palestinian-conflict/&usg=__1YEuvKE4A9r6IIRkcz9Pu6ahN8Q=&h=800&w=1280&sz=433&hl=en&start=1&sig2=ekqjELPNQsK-QQ2r-4TeeQ&zoom=1&tbnid=Xz9P1WD4o4TSlM:&tbnh=94&tbnw=150&ei=b8sDUq36Ge3figLCzoBY&itbs=1&sa=X&ved=0CCwQrQMwAA"> 

지금 여기의 출력 싹둑이다.

More 
Large 
Face 
Photo 
Clip art 
Line drawing 
Animated 
Past 24 hours 
Past week 
Reset tools 



















funny cats 
cats and kittens 
cats musical 
cute cats 
lots of cats 
cats with guns 
2 
3 
4 
5 
6 
7 
8 
9 
10 
Next 

공지 화면에있는 모든 공백? 거기서 빈 페이지의 ""링크가 pp 페이지 출력에 있습니다. 누구든지 클릭 할 수있는 방법에 대한 아이디어가 있습니까?

다음은 스크립트의 코드입니다.

require 'mechanize' 
agent = Mechanize.new 
page = agent.get('https://google.com') 
page = agent.page.link_with(:text => 'Sign in').click 
# pp page 
sign_in = page.form()  ##leave empty = nil 
sign_in.Email = '10halec' 
sign_in.Passwd = 'password' 
page = agent.submit(sign_in) 

page = agent.page.link_with(:text => 'Images').click 
search = page.form('f') 
search.q = 'cats' 
page = agent.submit(search) 

# pp page 

# agent.page.image_with(:src => /imgres?/).fetch.save 
page = agent.page.link_with(:text => '').click 
# pp page 

# page.links.each do |link| 
# puts link.text 
# end 
pp page 

def save filename = nil 
    filename = find_free_name filename 
    save! filename 
end 
+0

문제가있는 페이지에 관련 html을 게시하십시오. – 7stud

답변

1

공지 화면에있는 모든 공백? 그것은 빈 페이지 이름 인 ""링크가 pp 페이지 출력에있는 곳입니다. 누구든지 내가 할 수있는 아이디어가 있습니까 하나를 클릭하십시오?

페이지 = agent.page.link_with는 (: 텍스트 => '') 그 라인은 나를 위해 작동

을 .click.

page1.html :

<!DOCTYPE html> 
<html> 
    <head><title>Test</title></head> 
    <body> 
    <div><a href="/somesite.com/cat1.jpg">cat1</a></div> 
    <div><a href="/page2.html"></a></div> 
    <div><a href="/somesite.com/cat3.jpg"></a></div> 
    </body> 
</html> 

page2.html : 나는 내 로컬 아파치 서버의 htdocs에 디렉토리 (공개적으로 액세스 할 수있는 디렉토리)에 다음과 같은 HTML 페이지를 모두 넣어

<!DOCTYPE html> 
<html> 
    <head><title>Page2</title></head> 
    <body> 
    <div>hello</div> 
    </body> 
</html> 

그럼 내 서버를 시작 했으므로 URL을 사용하는 브라우저에서 page1.html에 액세스 할 수있었습니다.

http://localhost:8080/page1.html 

는 그럼 난 루비 프로그램 실행 :

require 'mechanize' 

agent = Mechanize.new 
agent.get('http://localhost:8080/page1.html') 
pp agent.page 

page = agent.page.link_with(:text => '').click 
puts page.title 

을 ... 그리고 출력했다 :

#<Mechanize::Page 
{url #<URI::HTTP:0x00000100c8dc18 URL:http://localhost:8080/page1.html>} 
{meta_refresh} 
{title "Test"} 
{iframes} 
{frames} 
{links 
    #<Mechanize::Page::Link "cat1" "/somesite.com/cat1.jpg"> 
    #<Mechanize::Page::Link "" "/page2.html"> 
    #<Mechanize::Page::Link "" "/somesite.com/cat3.jpg">} 
{forms}> 

Page2 

보호 프로파일 페이지 출력이 출력과 동일보고, 나는 클릭 할 성공적으로 수 있었다 텍스트가없는 링크 - 출력 Page2에 의해 입증 됨.

해당 코드의 유일한 문제점은 해당 link_with()가 첫 번째 일치 항목 만 반환한다는 것입니다.

require 'mechanize' 

agent = Mechanize.new 
agent.get('http://localhost:8080/page1.html') 

links = agent.page.links_with(:text => '') 
p links 

--output:-- 
[#<Mechanize::Page::Link "" "/page2.html"> 
, #<Mechanize::Page::Link "" "/somesite.com/cat3.jpg"> 
] 

나는 당신이 문제가있는 링크의 실제 HTML을보고 싶다 : 나는 links_with()를 사용하는 경우, 나는 모든 일치하는 링크를 얻을.

+0

감사합니다. 실제로 도움이되었습니다. 내 실수로 간단한 실수가 있었는데, links_with는 필자가 필요로하는 것이므로 클릭 할 수있는 색인 된 위치를 정의 할 수 있습니다. –