Use an example to illustrate, such as this http://video.sina.com.cn/v/b/75314002-1648211320.html.
Open it with Firefox, enable firebug, and get the following information.
which in 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
The response we get has the xml information we want, of which vid is the red part above, and everything after uid can be ignored. We directly enter http://v.iask.com/v_play.php?vid=75314002 in the browser You can still get the same information. Since then, the idea of parsing has become clear. Extract the vid from the video link, use http://v.iask.com/v_play.php?vid= to get the xml file, and parse the xml file to get the real video address.
The following is the code for parsing xml, using sax to parse xml. First define xml reader.
Copy the code code as follows:
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 only need to call the sax factory to instantiate the parser.
Copy the code code as follows:
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLSaxReader reader = new XMLSaxReader();
InputStream in_withcode = new ByteArrayInputStream(
xml.getBytes("UTF-8"));//xml is the xml file just obtained, type String
sp.parse(in_withcode, reader);
videos=reader.getVideos();//Get Video List
timeLength=reader.getLength();//Get the video length
System.out.println(videos);
There is a lot of other information in the xml file, which can be parsed out according to your needs.