UWP Webview获取从网页到阵列的链接 [英] UWP Webview get links from webpage into an array

查看:78
本文介绍了UWP Webview获取从网页到阵列的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能在UWP Win 10之前找到旧的过时答案。我知道如何用旧的方式做到这一点,但它给了我一些问题。

I can only find old outdated answers for before UWP Win 10. I know how to do it the old ways, but it is giving me problems.

我是什么到目前为止,请注意问题似乎在VB中,它没有通过标签名称命令执行元素,就像我被告知它应该。将其更改为内部HTML,它将使用整页填充html变量。所以我似乎无法自己获得链接。

What I have so far is below, note the problem seem to lie in the VB where it isn't doing the element by tag name command like I've been told it should. Change that to inner HTML though, and it will populate the html variable with the full page. So I just can't get the links themselves it seems.

任何帮助表示赞赏!谢谢!

Any help is appreciated! Thanks!

XAML

<Page
    x:Class="webviewMessingAround.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:webviewMessingAround"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <WebView x:Name="webview" Source="http://regsho.finra.org/regsho-December.html" DOMContentLoaded="WebView_DOMContentLoaded" />
            <Button x:Name="button" HorizontalAlignment="Left" Margin="145,549,0,0" VerticalAlignment="Top">
                <Button x:Name="button1" Click="button_Click" Content="Button" Height="58" Width="141"/>
            </Button>
        </Grid>
    </Grid>
</Page>

VB代码

 Private Async Sub webview_DOMContentLoaded(sender As WebView, args As WebViewDOMContentLoadedEventArgs) Handles webview.DOMContentLoaded

        Dim html = Await webview.InvokeScriptAsync("eval", ({"document.getElementsByTagName('a');"}))

        'Debug.WriteLine(html)

    End Sub


推荐答案

InvokeScriptAsync 只能返回脚本调用的字符串结果。

The InvokeScriptAsync can only return the string result of the script invocation.


返回值

此方法返回时,脚本调用的字符串结果。

When this method returns, the string result of the script invocation.

因此,如果您想从网页获取链接,则需要将所有链接放入要返回的字符串。对于C#示例:

So if you want get links form a web page, you need put all the links into a string to return. For a C# example:

string html = await webview.InvokeScriptAsync("eval", new string[] { "[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');" });
System.Diagnostics.Debug.WriteLine(html);

我在这里使用

[].map.call(document.getElementsByTagName('a'), function(node){ return node.href; }).join('||');

将所有链接放入字符串中。您可能需要更改此JavaScript代码以实现自己的代码。

to put all the links into a string. You may need to change this JavaScript code to implement your own.

之后,您可以将字符串拆分为如下数组:

After this you can split the string into a array like:

var links = html.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);

虽然我以C#为例,但VB代码应该相似。

Although I used C# for example, but the VB code should be similar.

这篇关于UWP Webview获取从网页到阵列的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆