我正在尝试使用将...
- 转到一个网页(特别是,Google会像这样公开发布的页面https://docs.google.com/spreadsheets/d/1ce9hte2rdgpsxmhj-pxokrgx_yeeocrjbtiovtla_yeocrjbtiovtla_2ii _2ii/pubhtml。
- 接下来,我希望它转到每个单独的URL找到页面,并爬网上的标题和主要图像。
- 理想情况下,如果Google表上的URL是例如特定的Wikipedia页面和Huffington Post文章,它将打印出:
链接:https://en.wikipedia.org/wiki/wolfenstein_3d
标题:Wolfenstein 3D
图片:https://en.wikipedia.org/wiki/wolfenstein_3d#/media/file:wolfenstein-3d.jpg链接:http://www.huffingtonpost.com/2012/01/02/ron-pippin_n_1180149.html
标题:罗恩·皮平(Ron Pippin(的神话档案包含所有事物的历史(照片(
图片:http://i.huffpost.com/gen/453302/pippin.jpg(请原谅URL中的空间。显然我不希望爬网添加空间并分解URL ...堆栈溢出只是我不会让我发布更多链接(
到目前为止,我已经使用此代码来完成了第一步的JSOUP(从初始URL中拉出链接(:
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 mycrawler {
public static void main(String[] args) {
Document doc;
try {
doc = Jsoup.connect("https://docs.google.com/spreadsheets/d/1CE9HTe2rdgPsxMHj-PxoKRGX_YEOCRjBTIOVtLa_2iI/pubhtml").get();
Elements links = doc.select("a[href]");
for (Element link : links) {
System.out.println(link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
我现在很难弄清楚如何创建crawler的第二个方面,它在每个链接中循环循环(可能是可变数量的链接(,并从每个链接中找到标题和主图像。
public static void main(String[] args) {
Document doc;
String url = "https://docs.google.com/spreadsheets/d/1CE9HTe2rdgPsxMHj-PxoKRGX_YEOCRjBTIOVtLa_2iI/pubhtml";
try {
doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]");
for (Element link : links) {
String innerurl = link.text();
if (!innerurl.contains("://")) {
continue;
}
System.out.println("*******");
System.out.println(innerurl);
Document innerDoc = Jsoup.connect(innerurl).get();
Elements headerLinks = innerDoc.select("h1");
for (Element innerLink : headerLinks) {
System.out.println("Headline : " + innerLink.text());
}
Elements imgLinks = innerDoc.select("img[src]");
for (Element innerLink : imgLinks) {
String innerImgSrc = innerLink.attr("src");
if(innerurl.contains("huffingtonpost") && innerImgSrc.contains("i.huffpost.com/gen")){
System.out.println("Image : " + innerImgSrc);
}
if(innerurl.contains("wikipedia")) {
Pattern pattern = Pattern.compile("(jpg)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(innerImgSrc);
if(matcher.find()){
System.out.println("Image : " + innerImgSrc);
break;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
Output
*******
https://en.wikipedia.org/wiki/Wolfenstein_3D
Headline : Wolfenstein 3D
Image : //upload.wikimedia.org/wikipedia/en/0/05/Wolfenstein-3d.jpg
*******
http://www.huffingtonpost.com/2012/01/02/ron-pippin_n_1180149.html
Headline : Ron Pippin's Mythical Archives Contain History Of Everything (PHOTOS)
Image : http://i.huffpost.com/gen/453302/PIPPIN.jpg
Image : http://i.huffpost.com/gen/453304/PIPSHIP.jpg
我认为您应该使用link.attr("href")
而不是link.text()
获得链接的href
属性。(在页面中,显示的文本和下面的HREF不同(在第二步中跟踪所有链接,并迭代该列表以获取相应的Document
,您可以从中提取标题和图像URL。
对于Wiki页面,我们可以使用Jsoup
提取标题,如下所示
Element heading = document.select("#firstHeading").first();
System.out.println("Heading : " + heading.text());