So i am trying to get some data from a website by using JSoup, and i am not sure how.
This is the code i have been using and it does not work:
public static Document doc;
public static Elements elementPrice;
public void getDocument()
{
try
{
doc = Jsoup.connect("https://steamcommunity.com/market/search?appid=730&q=ak47+jaguar+factory-new").get();
elementPrice = doc.select("market_table_value");
System.out.println(elementPrice);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
I am trying to get data from this site: https://steamcommunity.com/market/search?appid=730&q=ak47+jaguar+factory-new
And the data/attribute i am trying to get is this:
Pris från:
35,36€
Which is the price of a csgo item in steam.
And now i wonder why this doesen't work.
Thanks for any help! :)
select uses CSS selectors syntax so if you want to describe elements by its class use .className (notice dot at start). So try with
elementPrice = doc.select(".market_table_value");
// ^--add this dot
You can also use getElementsByClass method instead of select and pass name of class directly, without any CSS like
elementPrice = doc.getElementsByClass("market_table_value");
Related
I've been trying to create a program in Java that can catch the odds of a game from a sportsbook like FanDuel but I've been running into a lot of problems. When I print the html for the site I dont get the entire html for the site so Im unable to go into the divs and retrieve the actual data I want.
I used the Url https://sportsbook.fanduel.com/ . If I try and run a method like Elements element = doc.getElementByID("root"); to get the data inside that div the rest of the data in that div will not appear. enter image description here. I specifically would just like to get the moneyline data for each game if anyone can help that would be great
public class ExtractSportsBookData {
public static void extractData(String url){
try{
Document doc = Jsoup.connect(url).get();
String html = doc.html();
System.out.println(html);
} catch (IOException e){
e.printStackTrace();
}
}
}
enter image description here
If you look at the image inside the li tags is where the data is stored for the moneylines for each game but I cannot seem to find a way to extract that data using Jsoup
public class Main {
public static void main(String[] args) {
ExtractSportsBookData.extractData("https://sportsbook.fanduel.com/");
}
}
import java.io.IOException;
import java.io.SyncFailedException;
public class ExtractSportsBookData {
public static void extractData(String url){
try{
Document doc = Jsoup.connect(url).get();
String html = doc.html();
//System.out.println(html);
Elements element = doc.getElementsByClass("jo jp fk fe jy jz bs");
System.out.println(element.isEmpty());
} catch (IOException e){
e.printStackTrace();
}
}
}
enter image description here
The result I receive from this is true meaning that the element is empty which is not what I want. Any help on this would be appreciated
I'm trying to create a playlist viewer and I got stuck on paging.
I want to get all the names of current users playlists.
Any help would be appreciated.
public void getPlaylists() {
try {
SpotifyApi spotifyApi = new SpotifyApi.Builder().setAccessToken(auth.getToken()).build();
GetListOfCurrentUsersPlaylistsRequest getListOfCurrentUsersPlaylistsRequest = spotifyApi.getListOfCurrentUsersPlaylists().limit(5).offset(0).build();
Paging<PlaylistSimplified> playlistSimplifiedPaging = getListOfCurrentUsersPlaylistsRequest.execute();
} catch (IOException | SpotifyWebApiException e) {
System.out.println("Erroras: " + e.getMessage());
}
}
Okey so found out the answer myself don't know if its the correct way but it will work.
You can convert the paging object to a list :
List<PlaylistSimplified> playlists = Arrays.asList(playlistSimplifiedPaging.getItems());
And then get the info about your object normally.
I really need to parse this div.fan-details data (added link of a picture which shows exactly what I need). But I just don't know how to do it. I tried the following code, but it didn't return anything.
public class App {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://www.younow.com/name").get();
Elements info = doc.select("div.fan-details");
for (Element fansInfo: info) {
String str = fansInfo.toString();
System.out.println(str);
}
}
}
Sorry, I just started using jsoup. Any help is appreciated!
IMG:
The Object richtextarea of java gwt has as default the font-family 'times new roman' is it somehow possible to change the family to 'Arial'?
You have several options. You can create a CSS class and set it on the body element of the document inside RichTextArea, or you can set the style attribute on it directly. If you want this change to be consistent throughout your app, I recommend creating a new class and adding an InitializeHandler to it.
public class MyRichTextArea extends RichTextArea {
public MyRichTextArea() {
addInitializeHandler(new InitializeHandler() {
#Override
public void onInitialize(InitializeEvent ie) {
Document document = IFrameElement.as(getElement()).getContentDocument();
BodyElement body = document.getBody();
body.setAttribute("style", "font-family: Arial Unicode MS,Arial,sans-serif;");
});
}
}
}
It is better to use the provided functionality.
If you want to change it at creation time, you will need the initializeHandler as mentioned in answer 1:
RichTextArea rta = new RichTextArea();
rta.addInitializeHandler(new InitializeHandler() {
public void onInitialize(InitializeEvent event) {
rta.getFormatter().setFontName("\"Trebuchet MS\",Trebuchet,Arial");
rta.getFormatter().setFontSize(FontSize.SMALL);
rta.getFormatter().setForeColor("#FF0000");
}
});
PS: you need to do this before you add any content to the textarea, or it will only be applied to new input.
I've found more common solution:
addInitializeHandler(new InitializeHandler() {
public void onInitialize(InitializeEvent ie) {
Document document = IFrameElement.as(getElement()).getContentDocument();
BodyElement body = document.getBody();
HeadElement head = HeadElement.as(Element.as(body.getPreviousSibling()));
LinkElement styleLink = document.createLinkElement();
styleLink.setType("text/css");
styleLink.setRel("stylesheet");
styleLink.setHref("richtextarea.css");
head.appendChild(styleLink);
}
});
After adding initialization handler, it's easy to change styles in richtextarea.css file
We are trying to render both HTML and plain text using JTextPane. Basically the actual content is hosted on remote server, this content can contain some degree of HTML tags or none at all. In my JTextPane I have it defined as shown:
JTextPane jText = new JTextPane();
jText.setEditable(false);
jText.setContentType("text/html");
String content = "Please view article <a href=mydomain.com/content.txt>Link..</a>";
jText.setText(content);
And then using HyperlinkListener wants to be able to render the content when the link is clicked. I am doing so using the syntax below;
jText.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(final HyperlinkEvent he)
{
//Render the page
try{
setPage(he.getURL()); //Error on this line
}catch(Exception e){e.printStackTrace();}
}
});
Unfurtunately when we click on the link to render the content, we end up with the exception:
java.lang.IllegalArgumentException: Must be StyledEditorKit
at javax.swing.JTextPane.setEditorKit(JTextPane.java:474)
at javax.swing.JEditorPane.setContentType(JEditorPane.java:888)
at javax.swing.JEditorPane.getStream(JEditorPane.java:713)
at javax.swing.JEditorPane.setPage(JEditorPane.java:408)
This looks like when the content has no HTML tag available. Can someone help us resolve this issue so we can render both plain text and HTML.
Thanks in advance.
EDITED.
It sounds like what you're saying is that since you want to support both HTML and plain-text from your JTextPane as input, then not receiving a URL isn't really considered a problem. In that case, you should consider logging/eating the exception:
jText.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(final HyperlinkEvent he)
{
try {
//Render the page
setPage(he.getURL()); //Error on this line
} catch (IllegalArgumentException e) {
// either log the argument here, or just eat it and do nothing with it. logger.error() recommended
}
}
});