java如何使用Jsoup提取链接?
我正在使用JSoup爬网并获得结果。我想进行关键字搜索。比如我爬 http://www.business-standard.com/用于以下关键字:
google hyderabad
它应该为我提供链接:
我写了下面的代码,但没有给出适当的结果
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class App {
public static void main(String[] args) {
Document doc;
try {
doc = Jsoup.connect("http://www.business-standard.com").userAgent("Mozilla").get();
String title = doc.title();
System.out.println("title : " + title);
Elements links = doc.select("a:contains(google)");
for (Element link : links) {
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果如下:
title : India News, Latest News Headlines, BSE live, NSE Live, Stock Markets Live, Financial News, Business News & Market Analysis on Indian Economy - Business Standard News link : /photo-gallery/current-affairs/mumbai-central-turns-into-wi-fi-zone-courtesy-google-power-2574.htm text : Mumbai Central turns into Wi-Fi zone, courtesy Google power link : plus.google.com/+businessstandard/posts text : Google+
Jsoup 1.8.2
# 1 楼答案
请尝试以下url:
示例代码
输出
你要找的链接位于第二位
讨论
下面的示例代码只获取第一个结果页面。如果需要获取更多结果,请提取下一个链接页(
#hpcontentbox div.next-colum > a
),并使用Jsoup对其进行爬网您会注意到,在我提供给您的上述链接中还有其他参数:
itemPerPages
:不言自明(默认为19)page
:搜索结果页面索引(如果未提供,则默认为1)company-code
:??(可以是空的)你可以尝试给这个url赋予
itemPerPages
更大的值(100或更多)。这可能会减少你的爬行时间使用
absUrl
方法是为了获得绝对URL,而不是相对URL