简体   繁体   中英

How to extract a p value from a web page using Jsoup

I'm trying to extract a text in hebrew from a web page

https://www.sefaria.org/Berakhot.2a.2?lang=he&with=all&lang2=he

But the result is just the first P, so I tried with this option too:

Elements elements = document.select(".segmentNumber sans.content-section p");

But nothing happen.

Can you tell me what's wrong with the code, and how can I get all the P's elements from the web page?

Thanks.

this is the code

package scraping;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;

public class Scraping {

    public static void main(String[] args) throws IOException {
        try{
            Document document = Jsoup.connect("https://www.sefaria.org/Berakhot.2a?lang=he").get();
    
            System.out.println( document.text() );
            System.out.println("Selecting HTML tag name having specified class name");
            
            Elements elements = document.select("p.he");
            if(elements.size() > 0)
               System.out.println(elements.get(0));
            
        }
        catch(IOException ioe){
            System.out.println("Unable to connect to the URL");
        }
    }
}

I solve the problem, here's the code:

//Get content the google home page using Jsoup
  Document document = Jsoup.connect("https://www.sefaria.org/Berakhot.2a?lang=he").get();

  System.out.println("Selecting HTML tag name having specified class name");
  document.select("p.he").forEach(System.out::println);

However, how can I remove the tags from the output? see the output

Selecting HTML tag name having specified class name
<p lang="he" class="he"><big><strong>מֵאֵימָתַי</strong></big> קוֹרִין אֶת שְׁמַע בָּעֲרָבִין? מִשָּׁעָה שֶׁהַכֹּהֲנִים נִכְנָסִים לֶאֱכוֹל בִּתְרוּמָתָן. עַד סוֹף הָאַשְׁמוּרָה הָרִאשׁוֹנָה. דִּבְרֵי רַבִּי אֱלִיעֶזֶר. </p>
<p lang="he" class="he">וַחֲכָמִים אוֹמְרִים: עַד חֲצוֹת. </p>
<p lang="he" class="he">רַבָּן גַּמְלִיאֵל אוֹמֵר עַד שֶׁיַּעֲלֶה עַמּוּד הַשַּׁחַר. </p>
<p lang="he" class="he">מַעֲשֶׂה וּבָאוּ בָנָיו מִבֵּית הַמִּשְׁתֶּה, אָמְרוּ לוֹ: לֹא קָרִינוּ אֶת שְׁמַע, אָמַר לָהֶם: אִם לֹא עָלָה עַמּוּד הַשַּׁחַר חַיָּיבִין אַתֶּם לִקְרוֹת. וְלֹא זוֹ בִּלְבַד אָמְרוּ, אֶלָּא כָּל מַה שֶּׁאָמְרוּ חֲכָמִים ״עַד חֲצוֹת״, מִצְוָתָן עַד שֶׁיַּעֲלֶה עַמּוּד הַשַּׁחַר. </p>
<p lang="he" class="he">הֶקְטֵר חֲלָבִים וְאֵבָרִים, מִצְוָתָן עַד שֶׁיַּעֲלֶה עַמּוּד הַשַּׁחַר וְכָל הַנֶּאֱכָלִים לְיוֹם אֶחָד, מִצְוָתָן עַד שֶׁיַּעֲלֶה עַמּוּד הַשַּׁחַר. אִם כֵּן, לָמָה אָמְרוּ חֲכָמִים ״עַד חֲצוֹת״ — כְּדֵי לְהַרְחִיק אָדָם מִן הָעֲבֵירָה: </p>
<p lang="he" class="he"><big><strong>גְּמָ׳</strong></big> תַּנָּא הֵיכָא קָאֵי דְּקָתָנֵי ״מֵאֵימָתַי״? </p>
<p lang="he" class="he">וְתוּ: מַאי שְׁנָא דְּתָנֵי בְּעַרְבִית בְּרֵישָׁא? לִתְנֵי דְּשַׁחֲרִית בְּרֵישָׁא! </p>
<p lang="he" class="he">תַּנָּא אַקְּרָא קָאֵי, דִּכְתִיב: ״בְּשָׁכְבְּךָ וּבְקוּמֶךָ״. וְהָכִי קָתָנֵי: זְמַן קְרִיאַת שְׁמַע דִּשְׁכִיבָה אֵימַת? — מִשָּׁעָה שֶׁהַכֹּהֲנִים נִכְנָסִין לֶאֱכוֹל בִּתְרוּמָתָן. </p>
<p lang="he" class="he">וְאִי בָּעֵית אֵימָא: יָלֵיף מִבְּרִיָּיתוֹ שֶׁל עוֹלָם, דִּכְתִיב: ״וַיְהִי עֶרֶב וַיְהִי בֹקֶר יוֹם אֶחָד״. </p>
<p lang="he" class="he">אִי הָכִי, סֵיפָא דְּקָתָנֵי ״בַּשַּׁחַר מְבָרֵךְ שְׁתַּיִם לְפָנֶיהָ וְאַחַת לְאַחֲרֶיהָ, בָּעֶרֶב מְבָרֵךְ שְׁתַּיִם לְפָנֶיהָ וּשְׁתַּיִם לְאַחֲרֶיהָ״, לִתְנֵי דְּעַרְבִית בְּרֵישָׁא! </p>
<p lang="he" class="he">תַּנָּא פָּתַח בְּעַרְבִית, וַהֲדַר תָּנֵי בְּשַׁחֲרִית, עַד דְּקָאֵי בְּשַׁחֲרִית, פָּרֵישׁ מִילֵּי דְשַׁחֲרִית, וַהֲדַר פָּרֵישׁ מִילֵּי דְעַרְבִית. </p>
<p lang="he" class="he">אָמַר מָר מִשָּׁעָה שֶׁהַכֹּהֲנִים נִכְנָסִים לֶאֱכוֹל בִּתְרוּמָתָן. מִכְּדִי כֹּהֲנִים אֵימַת קָא אָכְלִי תְּרוּמָה? — מִשְּׁעַת צֵאת הַכּוֹכָבִים, לִתְנֵי: ״מִשְּׁעַת צֵאת הַכּוֹכָבִים״! </p>
<p lang="he" class="he">מִלְּתָא אַגַּב אוֹרְחֵיהּ קָמַשְׁמַע לַן — כֹּהֲנִים אֵימַת קָא אָכְלִי בִּתְרוּמָה — מִשְּׁעַת צֵאת הַכּוֹכָבִים. וְהָא קָמַשְׁמַע לַן דְּכַפָּרָה לָא מְעַכְּבָא. כִּדְתַנְיָא: ״וּבָא הַשֶּׁמֶשׁ וְטָהֵר״ — בִּיאַת שִׁמְשׁוֹ מְעַכַּבְתּוֹ מִלֶּאֱכוֹל בִּתְרוּמָה, וְאֵין כַּפָּרָתוֹ מְעַכַּבְתּוֹ מִלֶּאֱכוֹל בִּתְרוּמָה. </p>
<p lang="he" class="he">וּמִמַּאי דְּהַאי ״וּבָא הַשֶּׁמֶשׁ״ בִּיאַת הַשֶּׁמֶשׁ, וְהַאי ״וְטָהֵר״ — טְהַר יוֹמָא. </p>

And how can execute this from cmd on windows? When I try, this error appears:

C:\Users\Usuario\Documents\NetBeansProjects\Scraping\build\classes\scraping>java Scraping
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Scraping has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM