Jsoup ArrayList 和 LinkedHashMap 组合
Jsoup ArrayList and LinkedHashMap Combo
我编写的代码使用 jsoup 访问站点,查看所有段落标题,然后将它们保存到名为 headingList
的 ArrayList 中。这是棘手的部分。我有一张地图,它以字符串为键,以 ArrayLists 为值。代码的设计方式要求它转到多个页面。因此,标题数量以及与标题相关的段落数量可能会有很大差异。所以,这里的想法是创建两个 int 值。一个名为 headingAmt
的 int 值在它查看页面并确定有多少标题后设置。名为 headCount
的第二个 int 值被初始化为值 1。然后我要做的是像这样设置一个 while 循环:while(headCount != headAmt + 1)
并在循环结束时递增它,以便它在 headCount
遍历每个标题时终止。在 while 循环中,我尝试遍历并将每个段落添加到名为 items
的 ArrayList,然后获取项目 arrayList 中的内容,然后将其设置为地图中第一个项目的值。然后,清除 ArrayList,转到下一段,将其中的内容保存到 items
,然后将 ArrayList 设置为地图中第二项的值,依此类推。我有我可以使用的代码 post,但它令人困惑,因为有问题的 while 循环已经重新排列了很多次,因为我无法让它正常工作。
编辑以下代码,以防有人能提供帮助:
public class Finder {
public Finder(String url) {
String mainURL = "http://www.website.com";
Map<String, List<String> > headMap = new HashMap<>();
ArrayList<String> headingList = new ArrayList<>();
ArrayList<String> items = new ArrayList<>();
int headCounter = 1;
String itemList = "div > div:nth-child(1).category > ul:nth-child(2) > li.item > span";
int headAmt;
Document doc1 = null;
///// Connect to site to get menu /////
try{
doc1 = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
.referrer("http://www.google.com")
.get();
}
catch(IOException e){
System.out.println("Can't connect to website");
}
/////// Get headings ////////
Elements head = doc1.select("div > div > div > h3");
////// Loop through headings and add to ArrayList /////
for(Element e: head){
headingList.add(e.text());
}
headAmt = headingList.size();
/*
Here is the problem
*/
while(headCounter != headAmt + 1){
Elements elem = doc1.select("div > div:nth-child("+ headCounter +").category > ul:nth-child(2) > li.item > span");
for (String key : headingList) {
for(Element e : elem){
items.add(e.text());
}
List<String> value = new ArrayList<>(items);
headMap.put(key, value);
}
items.clear();
headCounter++;
}
}
}
}
}
您可以尝试这样的操作:
public class Finder {
public static void main(String[] args) {
new Finder(
"http://www.allmenus.com/ny/new-york/250087-forlinis-restaurant/menu/");
}
public Finder(String url) {
Document doc1 = null;
try {
doc1 = Jsoup
.connect(url)
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
.referrer("http://www.google.com").get();
} catch (IOException e) {
System.out.println("Can't connect to website");
}
Elements elements = doc1.select(".category");
HashMap<String, ArrayList<List<String>>> menu = new HashMap<String, ArrayList<List<String>>>();
for (Element e : elements) {
String name = e.select(".category_head>h3").first().text();
Elements itms = e.select("ul > li");
ArrayList<List<String>> menuItems = new ArrayList<List<String>>();
for (Element it : itms) {
menuItems.add(Arrays.asList(new String[] {
it.select("span").first().text(),
it.select("span").eq(1).text() }));
}
menu.put(name, menuItems);
}
for (String key : menu.keySet()) {
System.out.println(key);
ArrayList<List<String>> lst = menu.get(key);
for (List<String> item : lst) {
System.out.println(" " + item.get(0) + " " + item.get(1));
}
System.out.println("\n");
}
}
}
我编写的代码使用 jsoup 访问站点,查看所有段落标题,然后将它们保存到名为 headingList
的 ArrayList 中。这是棘手的部分。我有一张地图,它以字符串为键,以 ArrayLists 为值。代码的设计方式要求它转到多个页面。因此,标题数量以及与标题相关的段落数量可能会有很大差异。所以,这里的想法是创建两个 int 值。一个名为 headingAmt
的 int 值在它查看页面并确定有多少标题后设置。名为 headCount
的第二个 int 值被初始化为值 1。然后我要做的是像这样设置一个 while 循环:while(headCount != headAmt + 1)
并在循环结束时递增它,以便它在 headCount
遍历每个标题时终止。在 while 循环中,我尝试遍历并将每个段落添加到名为 items
的 ArrayList,然后获取项目 arrayList 中的内容,然后将其设置为地图中第一个项目的值。然后,清除 ArrayList,转到下一段,将其中的内容保存到 items
,然后将 ArrayList 设置为地图中第二项的值,依此类推。我有我可以使用的代码 post,但它令人困惑,因为有问题的 while 循环已经重新排列了很多次,因为我无法让它正常工作。
编辑以下代码,以防有人能提供帮助:
public class Finder {
public Finder(String url) {
String mainURL = "http://www.website.com";
Map<String, List<String> > headMap = new HashMap<>();
ArrayList<String> headingList = new ArrayList<>();
ArrayList<String> items = new ArrayList<>();
int headCounter = 1;
String itemList = "div > div:nth-child(1).category > ul:nth-child(2) > li.item > span";
int headAmt;
Document doc1 = null;
///// Connect to site to get menu /////
try{
doc1 = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
.referrer("http://www.google.com")
.get();
}
catch(IOException e){
System.out.println("Can't connect to website");
}
/////// Get headings ////////
Elements head = doc1.select("div > div > div > h3");
////// Loop through headings and add to ArrayList /////
for(Element e: head){
headingList.add(e.text());
}
headAmt = headingList.size();
/*
Here is the problem
*/
while(headCounter != headAmt + 1){
Elements elem = doc1.select("div > div:nth-child("+ headCounter +").category > ul:nth-child(2) > li.item > span");
for (String key : headingList) {
for(Element e : elem){
items.add(e.text());
}
List<String> value = new ArrayList<>(items);
headMap.put(key, value);
}
items.clear();
headCounter++;
}
}
}
}
}
您可以尝试这样的操作:
public class Finder {
public static void main(String[] args) {
new Finder(
"http://www.allmenus.com/ny/new-york/250087-forlinis-restaurant/menu/");
}
public Finder(String url) {
Document doc1 = null;
try {
doc1 = Jsoup
.connect(url)
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36")
.referrer("http://www.google.com").get();
} catch (IOException e) {
System.out.println("Can't connect to website");
}
Elements elements = doc1.select(".category");
HashMap<String, ArrayList<List<String>>> menu = new HashMap<String, ArrayList<List<String>>>();
for (Element e : elements) {
String name = e.select(".category_head>h3").first().text();
Elements itms = e.select("ul > li");
ArrayList<List<String>> menuItems = new ArrayList<List<String>>();
for (Element it : itms) {
menuItems.add(Arrays.asList(new String[] {
it.select("span").first().text(),
it.select("span").eq(1).text() }));
}
menu.put(name, menuItems);
}
for (String key : menu.keySet()) {
System.out.println(key);
ArrayList<List<String>> lst = menu.get(key);
for (List<String> item : lst) {
System.out.println(" " + item.get(0) + " " + item.get(1));
}
System.out.println("\n");
}
}
}