Java parses the sine of a video

  • 2020-04-01 02:45:54
  • OfStack

Use an example to illustrate, like the http://video.sina.com.cn/v/b/75314002-1648211320.html.
Open it with firefox, open firebug, and get the following information.

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201401/20140110111127.jpg? 2014010111533 ">

Among them is this request

http://v.iask.com/v_play.php? vid=75314002&uid=1648211320&pid=478&tid=&plid=4001&prid=ja_7_3485822616&referrer=&ran=0.2936802236363292&r=video.sina.com.cn

Get a response in XML information we want, the vid is above the red part, behind the uid can be ignored, we directly enter http://v.iask.com/v_play.php? in your browser Vid =75314002 still gives you the same information. Since then, the idea of parsing has become clear. Extract the vid from the video link and use http://v.iask.com/v_play.php? Vid = get the XML file, parse the XML file to get the real video address.

Here's the code that parses XML, using sax. First, define an XML reader.


package hdu.fang.parser;

import hdu.fang.model.Video;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLSaxReader extends DefaultHandler {
    private List<Video> videos = null;
    private Video video = null;
    private Long timeLength = null;
    private String tag = null;

    @Override
    public void startDocument() throws SAXException {
        videos = new ArrayList<Video>();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if ("durl".equals(qName)) {
            video = new Video();
        }

        tag = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if ("durl".equals(qName)) {
            videos.add(video);
            video = null;
        }
        tag = null;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (tag != null) {
            String data = new String(ch, start, length);
            if ("timelength".equals(tag)) {
                timeLength = Long.valueOf(data);
            } else if ("order".equals(tag)) {
                video.setOrder(Integer.valueOf(data));
            } else if ("url".equals(tag)) {
                video.setUrl(data);
            } else if ("length".equals(tag)) {
                video.setLength(Integer.valueOf(data));
            }
        }
    }

     
    public List<Video> getVideos() {
        return videos;
    }

    public long getLength() {
        return timeLength;
    }
}

The Video class is a data model defined by myself. In the main function we simply call the sax factory instance resolver.


SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLSaxReader reader = new XMLSaxReader();
InputStream in_withcode = new ByteArrayInputStream(
    xml.getBytes("UTF-8"));//XML is just the XML file we just got, type String
sp.parse(in_withcode, reader);
videos=reader.getVideos();//Get the Video List
timeLength=reader.getLength();//Get the length of the video
System.out.println(videos);

There is a lot of other information in the XML file that you can parse out to see what you need.


Related articles: