我做了一个webrequest
得到一个网站的htmlcode然后我提取
htmlagilitypack
所需的链接
像这样:
'webrequest'
Dim rt As String = TextBox1.Text
Dim wRequest As WebRequest
Dim WResponse As WebResponse
Dim SR As StreamReader
wRequest = FtpWebRequest.Create(rt)
WResponse = wRequest.GetResponse
SR = New StreamReader(WResponse.GetResponseStream)
rt = SR.ReadToEnd
TextBox2.Text = rt
'htmlagility to extract the links'
Dim htmlDoc1 As New HtmlDocument()
htmlDoc1.LoadHtml(rt)
Dim links = htmlDoc1.DocumentNode.SelectNodes("//*[@id='catlist-listview']/ul/li/a")
Dim hrefs = links.Cast(Of HtmlNode).Select(Function(x) x.GetAttributeValue("href", ""))
'join the `hrefs`, separated by newline, into one string'
textbox3.text = String.Join(Environment.NewLine, hrefs)
链接是这样的:
http://wantedlink1
http://wantedlink2
http://wantedlink3
http://wantedlink4
http://wantedlink5
http://wantedlink6
http://wantedlink7
现在我想将字符串中的每一行添加到列表框而不是文本框中 每行一项
大约有400 http://wantedlink
答案 0 :(得分:1)
hrefs
已包含IEnumerable(Of String)
。将它们连接成一个字符串,然后再将它拆分以使其工作是很奇怪的。由于String.Split()
返回数组,因此您可能只需要将hrefs
投影到数组中以使.AddRange()
生效:
ListBox1.Items.AddRange(hrefs.ToArray())
答案 1 :(得分:0)
答案 2 :(得分:0)
可以,我找到了答案
Dim linklist = String.Join(Environment.NewLine, hrefs)
Dim parts As String() = linklist.Split(New String() {Environment.NewLine},
StringSplitOptions.None)
ListBox1.Items.AddRange(parts)
这会将所有400个链接添加到列表框