【问题标题】:Android: get link from HTML-code (no tag/id)Android:从 HTML 代码获取链接(无标签/ID)
【发布时间】:2015-01-30 13:43:23
【问题描述】:

我想download an iCal file to my app.它工作正常,但是这个文件的链接会change every 6 months. 继承文件链接的页面/链接不会改变,所以我想看看进入main page and find the link to the iCal file to download it.的代码

我认为这可能最适用于 javascript,但我对此一无所知。也没有我可以搜索的标签或 ID。

这是搜索链接的页面:

https://stundenplan.hs-furtwangen.de/splan/std?act=tt&lan=de&pu=-1&sel=pg&og=1433&pg=CNB4

搜索模式是 "/splan/ical" 我找到了文件的链接。

最后我需要 "/splan/ical?type=pg&puid=6&pgid=1617&lan=de" 存储在某个地方。

现在我只是使用Downloadmanager 来获取文件,没有任何 html 代码存储在任何地方。

希望有人可以提供帮助。谢谢。

编辑:

这是包含链接(第一个href)的html源代码部分:

<tr>
	<td />
	<td colspan="1"><a
		href="/splan/ical?type=pg&amp;puid=8&amp;pgid=2505&amp;lan=de"><img
			style="align: middle; border: 0;" src="/splan/pictures/ical.png"
			alt="ics feed" height="20" /></a> 


<a href="http://www.progotec.de/site/splandok/iCal-Anbindung"
	target="_blank"><img style="align: middle; border: 0;"
	alt="Hilfe zu ICal"
	src="/splan/pictures/hilfe.png"
	title="Hilfe zu ICal" height="20" /></a>
</td>
</tr>

【问题讨论】:

  • 你最好把包含链接的html源代码部分贴出来。
  • 我认为您的应用程序中没有任何 JavaScript 框架,这就是为什么最好只下载页面并使用正则表达式在下载的页面上搜索而不是在“在线”上使用 JS ” 页。
  • 感谢您的回答@Eun:您的意思是将整个html代码保存在某处并使用正则表达式搜索链接吗?你有例子吗?
  • @Eun:好的,现在我的头被正则表达式弄疼了。我无法让它工作。我尝试使用下面来自winner_joiner 的正则表达式。但它不会起作用。我的模式是“/splan/ical[^\”]*”。(如何在 cmets 中发布代码?)

标签: javascript android html search


【解决方案1】:

我不知道你在找什么,但是通过这个 javscript-script 你可以找到链接。

//like this you could look for the link on the page
//search done with Regularexpression
alert(document.body.innerHTML.match(/(\/splan\/ical[^"]*)/gi));
<body>
<!-- THIS WOULD HAVE TO BE THE BODY OF THE PAGE -->
  ...
  <a
		href="/splan/ical?type=pg&amp;puid=8&amp;pgid=2505&amp;lan=de"><img
			style="align: middle; border: 0;" src="/splan/pictures/ical.png"
			alt="ics feed" height="20" /></a> 
  ...
</body>

这里是整个stundenplan.hs-furtwangen.de 的正则表达式演示:http://regex101.com/r/wF3wU4/1

【讨论】:

  • 这看起来很有趣。 1. 我需要将此字符串保存在某处。我可以这样做 String link = document.body.innerHTML.match(/(\/splan\/ical[^"]*)/gi); 吗?2.“文档”是我保存html的对象-代码?
  • 广告 1. 是的,你可以。广告 2.document 是 HTML DOM 文档。如果您在字符串/变量中有“html-code”,那么link = codeStringVariable.match(/(\/splan\/ical[^"]*)/gi) 就足够了(使用 javscript 时)。
猜你喜欢
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 2018-06-02
  • 2011-01-26
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多