使用Jsoup从表格和网站的所有选项卡获取链接

问题描述 投票:0回答:1

我是网络抓取的新手,所以这个问题可能没有完美构建。我试图从某个给定页面中提取所有药物名称链接,结果提取所有az药物链接,然后迭代这些链接从这些链接中提取信息,如通用名称,品牌等。我有一个非常基本的下面的代码不起作用。将非常感谢一些帮助解决这个问题。

public class WebScraper {
  public static void main(String[] args) throws Exception {

    String keyword = "a"; //will iterate through all the alphabets eventually
    String url = "http://www.medindia.net/drug-price/brand-index.asp?alpha=" + keyword; 

    Document doc = Jsoup.connect(url).get();
    Element table = doc.select("table").first();
    Elements links = table.select("a[href]"); // a with href
    for (Element link : links) {
    System.out.println(link.attr("href"));
  }
}
java web-scraping jsoup
1个回答
1
投票

在查看了网站以及您期望得到的内容之后,看起来您正在抓取错误的表格元素。你不想要第一个表,你想要第二个表。

要获取特定的表,您可以使用:

Element table = doc.select("table").get(1);

这将获得索引1处的表,即文档中的第二个表。

© www.soinside.com 2019 - 2024. All rights reserved.