2016-11-23 102 views
0

好吧,我完成了我的Yelp掃描儀,一切都運行良好。我想現在要做的就是讓程序檢索每一個環節每一個企業的網址,進入該頁面,並掃描其是否包含:如何使用Jsoup從網站上的鏈接檢索網址?

xlink:href="#30x30_bullhorn"></use>

我非常有好吧,我將如何去做這件事,但是,我似乎無法找到一個jSoup方法來檢索鏈接的URL。在頁面的HTML中是否有地址會有url?我對HTML不太熟練,所以我看到的90%都是胡言亂語。這裏有一個例子鏈接,如果你想看看我指的是什麼。

https://www.yelp.com/search?find_loc=nj&start=10是主頁,我需要獲取頁面https://www.yelp.com/biz/la-cocina-newark的網址。橙色的擴音器就是我試圖讓它恢復的東西。這裏是我的代碼BTW:

import java.util.ArrayList; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 
import java.io.IOException; 
import java.util.Scanner; 

public class YelpScrapper 
{ 
    public static void main(String[] args) throws IOException, Exception 
    {   
     //Variables 
     String description; 
     String location; 
     int pages; 
     int parseCount = 0; 
     Document document; 

     Scanner keyboard = new Scanner(System.in); 

     //Perform a Search 
     System.out.print("Enter a description: "); 
     description = keyboard.nextLine(); 

     System.out.print("Enter a state: "); 
     location = keyboard.nextLine(); 

     System.out.print("How many pages should we scan? "); 
     pages = keyboard.nextInt(); 

     String descString = "find_desc=" + description.replace(' ', '+') + "&"; 
     String locString = "find_loc=" + location.replace(' ', '+') + "&"; 
     int number = 0; 

     String url = "https://www.yelp.com/search?" + descString + locString + "start=" + number; 
     ArrayList<String> names = new ArrayList<String>(); 
     ArrayList<String> address = new ArrayList<String>(); 
     ArrayList<String> phone = new ArrayList<String>(); 

     //Fetch Data From Yelp 
     for (int i = 0 ; i <= pages ; i++) 
     { 

      document = Jsoup.connect(url).get(); 

      Elements nameElements = document.select(".indexed-biz-name span"); 
      Elements addressElements = document.select(".secondary-attributes address"); 
      Elements phoneElements = document.select(".biz-phone"); 

      for (Element element : nameElements) 
      { 
       names.add(element.text()); 
      } 

      for (Element element : addressElements) 
      { 
       address.add(element.text()); 
      } 

      for (Element element : phoneElements) 
      { 
       phone.add(element.text()); 
      } 

      for (int index = 0 ; index < 10 ; index++) 
      { 
       System.out.println("\nLead " + parseCount); 
       System.out.println("Company Name: " + names.get(parseCount)); 
       System.out.println("Address: " + address.get(parseCount)); 
       System.out.println("Phone Number: " + phone.get(parseCount)); 

       parseCount = parseCount + 1; 
      } 

      number = number + 10; 

     } 
    } 
} 

回答

0

瞭解如何使用Chrome的開發者工具檢查元素,因爲它使得它非常容易找到的DOM元素(你說你不熟悉HTML,那麼你肯定將在此之後,並使用Inspect是一個偉大的學習工具)。將檢查員集中在「立即查看」按鈕上,您會看到:

<a href="https://www.yelp.com/biz_redir?cachebuster=1479918865&amp;s=1c73b4bdc9110f6e6dc72fff48cd6379d6eaac0cd6d15794a9414e546ad5a927&amp;src_bizid=U2eO8yFSc9YTf_SPnog8cw&amp;url=http%3A%2F%2Fwww.lacocinanewark.com%2F%23%21menu%2Fcl69&amp;website_link_type=cta" rel="nofollow" target="_blank" class="ybtn ybtn--primary ybtn--small ybtn-cta" data-component-bound="true">View Now</a>

你必須弄清楚如何遍歷這個,childNodes()將有助於遍歷。然後,您可以使用getElementsByClass("ybtn ybtn--primary ybtn--small ybtn-cta")來獲取鏈接所在的特定類,然後使用Element類的.attr()方法獲取href:.attr("href");

+0

檢查工具已幫助噸!它很精確地突出了它在頁面上的位置,所以我確切地知道在哪裏看。 –

+0

@BrandonWoodruff。現代網頁如此複雜,以至於在沒有類似檢查員的情況下建造任何類型的刮板都是可怕的。 –