如何使用 anglesharp 从 pagesource 获取所有 img 标签
How to get all img tags from pagesource with anglesharp
我确实使用 webview 获取完整页面源并将其写入文件
所以现在我需要从文件中提取所有图像
我使用 StreamReader 读取文件,并将其提供给字符串 Called TheHtmlSource,正如您在代码中看到的那样
这是我的代码
var config = Configuration.Default.WithDefaultLoader();
for (int i = 0; i < TheHtmlSource.Length; i++)
{
string theImageUrl = (await BrowsingContext.New(config).OpenAsync(TheHtmlSource))
.DocumentElement.Descendents()
.Where(x => x.NodeType == NodeType.Element)
.OfType<IHtmlImageElement>()
.Where(x => x.Attributes["class"]?.Value == "_icyx7")
.Select(x => x.Attributes["src"]?.Value)
.FirstOrDefault();
int index = theImageUrl.IndexOf("?");
string fixedImageUrl = theImageUrl.Remove(index);
_list.Add(new AllImageUrls()
{
url = fixedImageUrl
});
}
但我不工作,我一直收到 NullReference 错误
我在 Visual Studio
中使用 Xamarin
任何帮助将不胜感激
await BrowsingContext.New(config).OpenAsync(addressString)
接受一个字符串,它应该是一个地址。根据您的描述,您正在将源字符串传递给此方法,这是无效的。
如果要解析源字符串,可以使用AngleSharp.Parser.Html.HtmlParser
,具体用法可参考AngleSharp Documentation。
我确实使用 webview 获取完整页面源并将其写入文件
所以现在我需要从文件中提取所有图像
我使用 StreamReader 读取文件,并将其提供给字符串 Called TheHtmlSource,正如您在代码中看到的那样
这是我的代码
var config = Configuration.Default.WithDefaultLoader();
for (int i = 0; i < TheHtmlSource.Length; i++)
{
string theImageUrl = (await BrowsingContext.New(config).OpenAsync(TheHtmlSource))
.DocumentElement.Descendents()
.Where(x => x.NodeType == NodeType.Element)
.OfType<IHtmlImageElement>()
.Where(x => x.Attributes["class"]?.Value == "_icyx7")
.Select(x => x.Attributes["src"]?.Value)
.FirstOrDefault();
int index = theImageUrl.IndexOf("?");
string fixedImageUrl = theImageUrl.Remove(index);
_list.Add(new AllImageUrls()
{
url = fixedImageUrl
});
}
但我不工作,我一直收到 NullReference 错误
我在 Visual Studio
中使用 Xamarin任何帮助将不胜感激
await BrowsingContext.New(config).OpenAsync(addressString)
接受一个字符串,它应该是一个地址。根据您的描述,您正在将源字符串传递给此方法,这是无效的。
如果要解析源字符串,可以使用AngleSharp.Parser.Html.HtmlParser
,具体用法可参考AngleSharp Documentation。