Java Can';t获取循环的外部值
在for循环的外面,我不能得到所有的值,我只得到最后一个值,代码如下Java Can';t获取循环的外部值,java,Java,在for循环的外面,我不能得到所有的值,我只得到最后一个值,代码如下 public static void getHTMLElements(List<String> urls) throws IOException { getElements(urls); for (Map.Entry<String, HtmlElements> entry1 : urlList.entrySet()) {
public static void getHTMLElements(List<String> urls) throws IOException {
getElements(urls);
for (Map.Entry<String, HtmlElements> entry1 : urlList.entrySet()) {
HtmlElements htmlele = entry1.getValue();
System.out.println("url is " + entry1.getKey());
System.out.println("Element Name is " + htmlele.getElementName());
System.out.println("Attributes are " + htmlele.getAttributes());
}
}
public static void getElements(List<String> urls) throws IOException {
try {
for (int i = 0; i < urls.size(); i++) {
String s = urls.get(i);
Document doc = Jsoup.connect(s).get();
getInputElements(doc, s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void getInputElements(Document doc, String urls) {
// List l=new ArrayList();
HtmlElements htmlElements = new HtmlElements();
Properties attributes = new Properties();
Elements elements = doc.getAllElements();
for (Element element : elements) {
if (!element.tagName().contains("script")) {
String elementName = element.tagName();
Attributes attr = element.attributes();
for (Attribute attr1 : attr) {
if (attr1.getKey().contains("id")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("name")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("type")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
}
htmlElements.setElementName(elementName);
htmlElements.setAttributes(attributes);
}
urlList.put(urls, htmlElements);
}
}
}
publicstaticvoid getHTMLElements(列表URL)引发IOException{
获取元素(URL);
对于(Map.Entry entry1:urlList.entrySet()){
HtmlElements htmlele=entry1.getValue();
System.out.println(“url是”+entry1.getKey());
System.out.println(“元素名为”+htmlele.getElementName());
System.out.println(“属性是”+htmlele.getAttributes());
}
}
公共静态void getElements(列表URL)引发IOException{
试一试{
对于(int i=0;i
在上面的代码中,我试图从网站url获取所有元素。但是,elementName无法正确获取。仅获取最后一个值。我希望获取for循环之外的值。我假设您希望elementName包含具有“script”的元素的名称。在这种情况下,当您找到元素时,您需要停止循环。在if语句中添加break
public static void getHTMLElements(List<String> urls) throws IOException {
getElements(urls);
for (Map.Entry<String, HtmlElements> entry1 : urlList.entrySet()) {
HtmlElements htmlele = entry1.getValue();
System.out.println("url is " + entry1.getKey());
System.out.println("Element Name is " + htmlele.getElementName());
System.out.println("Attributes are " + htmlele.getAttributes());
}
}
public static void getElements(List<String> urls) throws IOException {
try {
for (int i = 0; i < urls.size(); i++) {
String s = urls.get(i);
Document doc = Jsoup.connect(s).get();
getInputElements(doc, s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void getInputElements(Document doc, String urls) {
// List l=new ArrayList();
HtmlElements htmlElements = new HtmlElements();
Properties attributes = new Properties();
Elements elements = doc.getAllElements();
for (Element element : elements) {
if (!element.tagName().contains("script")) {
String elementName = element.tagName();
Attributes attr = element.attributes();
for (Attribute attr1 : attr) {
if (attr1.getKey().contains("id")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("name")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("type")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
}
htmlElements.setElementName(elementName);
htmlElements.setAttributes(attributes);
}
urlList.put(urls, htmlElements);
}
}
}
如果(!element.tagName()包含(“脚本”)){
elementName=element.tagName();
打破
}
因为String elementName
只分配了for循环中的最后一个值。如果需要所有的标记名
,请声明列表标记名=新的ArrayList()代码>收集和保存
public static void getHTMLElements(List<String> urls) throws IOException {
getElements(urls);
for (Map.Entry<String, HtmlElements> entry1 : urlList.entrySet()) {
HtmlElements htmlele = entry1.getValue();
System.out.println("url is " + entry1.getKey());
System.out.println("Element Name is " + htmlele.getElementName());
System.out.println("Attributes are " + htmlele.getAttributes());
}
}
public static void getElements(List<String> urls) throws IOException {
try {
for (int i = 0; i < urls.size(); i++) {
String s = urls.get(i);
Document doc = Jsoup.connect(s).get();
getInputElements(doc, s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void getInputElements(Document doc, String urls) {
// List l=new ArrayList();
HtmlElements htmlElements = new HtmlElements();
Properties attributes = new Properties();
Elements elements = doc.getAllElements();
for (Element element : elements) {
if (!element.tagName().contains("script")) {
String elementName = element.tagName();
Attributes attr = element.attributes();
for (Attribute attr1 : attr) {
if (attr1.getKey().contains("id")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("name")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("type")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
}
htmlElements.setElementName(elementName);
htmlElements.setAttributes(attributes);
}
urlList.put(urls, htmlElements);
}
}
}
if (!element.tagName().contains("script")) {
tagNames.add(element.tagName());
}
这样,列表中就有了所有标记名。当循环循环时,该值会被覆盖。同样,您可以看到循环外的最后一个值。当您使用值设置变量时,它将替换该值。尝试将print语句移动到循环中。您想做什么?您是指String[]elementNames吗?我不明白你的问题是什么。请检查我的完整代码。我想从for循环的外部获取这些值谢谢,它正在工作。但是,如果你看到我上面的代码,我如何从for循环的外部获取属性值。你能指定HtmlElements
class吗?我知道HtmlElement
但不知道HtmlElement
类
public static void getHTMLElements(List<String> urls) throws IOException {
getElements(urls);
for (Map.Entry<String, HtmlElements> entry1 : urlList.entrySet()) {
HtmlElements htmlele = entry1.getValue();
System.out.println("url is " + entry1.getKey());
System.out.println("Element Name is " + htmlele.getElementName());
System.out.println("Attributes are " + htmlele.getAttributes());
}
}
public static void getElements(List<String> urls) throws IOException {
try {
for (int i = 0; i < urls.size(); i++) {
String s = urls.get(i);
Document doc = Jsoup.connect(s).get();
getInputElements(doc, s);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void getInputElements(Document doc, String urls) {
// List l=new ArrayList();
HtmlElements htmlElements = new HtmlElements();
Properties attributes = new Properties();
Elements elements = doc.getAllElements();
for (Element element : elements) {
if (!element.tagName().contains("script")) {
String elementName = element.tagName();
Attributes attr = element.attributes();
for (Attribute attr1 : attr) {
if (attr1.getKey().contains("id")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("name")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
if (attr1.getKey().contains("type")) {
attributes.put(attr1.getKey(), attr1.getValue());
}
}
htmlElements.setElementName(elementName);
htmlElements.setAttributes(attributes);
}
urlList.put(urls, htmlElements);
}
}
}