我是Jsoup的新手,我试图从onclick属性获取URL,该属性调用一个名为ga
的函数,该函数具有五个参数,所以看起来像ga('send', 'event', 'tunein', 'playjp', 'http://link that i want to get');
,我想抓一下http
网址。
我尝试使用attr("onclick")
选项,但它根本无法正常工作,您知道是否有机会以某种方式获得此功能。
答案 0 :(得分:1)
确定您在正确的节点上吗?
node.attr(“ onclick”)应该可以工作
您可以张贴您要剪贴的页面的链接,以及如何到达该节点吗?
public void jsoupParse() throws IOException {
Document doc = Jsoup.connect("https://www.internet-radio.com/station/dougeasyhits/").get();
Element image = doc.select("div.jp-controls").select("i").get(0); //get the first image (play button)
String onclick = image.attr("onclick");
System.out.print(onclick);
}
输出:
ga('send', 'event', 'tunein', 'playjp', 'http://airspectrum.cdnstream1.com:8114/1648_128.m3u');
现在您要做的就是使用'split'方法处理字符串以提取网址:
Document doc = Jsoup.connect("https://www.internet-radio.com/station/dougeasyhits/").get();
Element image = doc.select("div.jp-controls").select("i").get(0); //get the first image (play button)
String onclick = image.attr("onclick");
String[] parts = onclick.split("'"); //i split the string in an array of strings using [ ' ] as separator
String url = parts[9]; //the url is contained in the 10th element of the array
System.out.println(onclick);
System.out.print(url);
输出
ga('send', 'event', 'tunein', 'playjp', 'http://airspectrum.cdnstream1.com:8114/1648_128.m3u');
http://airspectrum.cdnstream1.com:8114/1648_128.m3u
如果您感到困惑,这就是“ onclick”属性的拆分方式:
parts[0] : "ga("
parts[1] : "send"
parts[2] : ", "
parts[3] : "event"
parts[4] : ", "
parts[5] : "tunein"
parts[6] : ", "
parts[7] : "playjp"
parts[8] : ", "
parts[9] : "http://airspectrum.cdnstream1.com:8114/1648_128.m3u"
parts[10] : ");"