只有href =“#”,没有onclick(),如何在脚本中加载它?

时间:2018-11-15 14:38:44

标签: javascript html web-scraping web-crawler href

我正在为网站https://www.welt.de上的文章编写抓取工具。我也想发表评论。但是,加载页面时,并非所有注释都会自动加载。取而代之的是,必须单击一个链接来加载更多评论,直到某个时候所有评论都被加载。

例如:https://www.welt.de/finanzen/immobilien/article183878020/Bundesbank-sieht-im-Immobilienboom-ein-Stabilitaetsrisiko.html

向下滚动时,会出现一个表面“ MEHR KOMMENTARE ANZEIGEN”(德语为“显示更多评论”)。

此链接如下:

<div href="#" style="text-align: center; height: 44px; cursor: pointer;">
<a style="font-size: 0.6875rem; font-family: ffmark, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: 800; color: rgb(0, 57, 91); line-height: 5;">
<span style="font-size: 0.6875rem; font-family: ffmark, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; font-weight: 500; margin-right: 0.625rem; text-align: right; color: rgb(120, 120, 120);">
MEHR KOMMENTARE ANZEIGEN
<span style="width: 14px; height: 8px; margin: 0px 0px 0px 0.625rem; padding-top: 0px; display: inline-block; vertical-align: initial;">
<svg viewBox="0 0 15 9" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g transform="translate(-608.000000, -4318.000000)" fill="#787878">
<polygon transform="translate(615.205882, 4322.852941) rotate(-90.000000) translate(-615.205882, -4322.852941) " points="618.264706 4315.79412 611.205882 4322.85353 618.264706 4329.91176 619.205882 4328.97059 613.088824 4322.85353 619.205882 4316.73529">
</polygon>
</g>
</g>
</svg>
</span>
</span>
</a>
</div>

但是,我不知道如何在脚本中加载此链接?

我了解到href="#"是在用javascript处理链接时使用的,它的样式不好,因为它仅用于更改鼠标的外观,对此还有其他方法。

但是onClick()方法在哪里? Kinda傻眼了...

2 个答案:

答案 0 :(得分:1)

点击两次显示评论会给我以下网址

https://api-co.la.welt.de/api/comments?document-id=183878020&created-cursor=2018-11-15T13:52:41.714&sort=NEWEST
https://api-co.la.welt.de/api/comments?document-id=183878020&created-cursor=2018-11-15T12:23:26.896&sort=NEWEST

哪个返回评论。因此,只要使用您拥有的帖子ID并不断摆弄created-cursor,直到获得所有评论为止即可。

编辑: 删除creator-cursor参数应该会给您所有注释

https://api-co.la.welt.de/api/comments?document-id=183878020

编辑2:

正如其他人提到的那样,如果不先联系网站所有者,这可能不是一个好主意。

答案 1 :(得分:0)

就找到点击处理程序而言:

如果您检查此元素,则可以看到它具有click事件处理程序,该事件处理程序在communityweb.js中调用了某些内容:

enter image description here

几乎可以肯定这是其他地方(例如document.getElementById('something').addEventListener("click", function(){ ... } );)附带的javascript

如果需要,您可以按照以下步骤进行操作并查看其调用的代码(请确保使用“ pretty print”功能,因为它已被最小化):

enter image description here

它从那里变得很复杂,但是如果您有足够的决心,则可以逐步进入调试器并查看正在调用的内容。