我想抓取所有网址的页面并将它们放入字典中.我用字典创建了一个类.但我似乎无法在其中添加元素.
type crawler =
new()= {}
member this.urls = new Dictionary<string,string>()
member this.start (url : string)=
let hw = new HtmlWeb()
let doc = hw.Load(url)
let docNode = doc.DocumentNode
let links = docNode.SelectNodes(".//a")
for aLink in links do
let href = aLink.GetAttributeValue("href"," ")
if href.StartsWith("http://") && href.EndsWith(".html") then
this.urls.Add(href, href)
Run Code Online (Sandbox Code Playgroud)
为什么字典网址是空的?
因为这里的url是在每次调用时都返回新字典的属性.
type Crawler() =
let urls = new Dictionary<string,string>()
member this.Urls = urls
member this.Start (url : string)=
let hw = new HtmlWeb()
let doc = hw.Load(url)
let docNode = doc.DocumentNode
let links = docNode.SelectNodes(".//a")
for aLink in links do
let href = aLink.GetAttributeValue("href"," ")
if href.StartsWith("http://") && href.EndsWith(".html") then
urls.Add(href, href)
Run Code Online (Sandbox Code Playgroud)