環境はVB.net 2003です。
URLを抽出することができたのでそれをテキストファイルに保存しようと
思ったのですが、メモ帳ができるだけでデータが保存されません。
初心者の質問ですがよろしくお願いします。
Private Sub downButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles downButton.Click
Try
Dim downloadurl As String = urlText.Text
Dim filepath As String = filePathText.Text
Dim downloaddirurl As String = downloadurl.Substring(0, downloadurl.LastIndexOf("/") + 1)
Dim webClient As WebClient = New WebClient
webClient.DownloadFile(downloadurl, "c:\temp.html")
'c:\\temp.htmlの内容をcontentsに格納
Dim reader As StreamReader = File.OpenText("c:\temp.html")
Dim contents As String = reader.ReadToEnd()
reader.Close()
'contentsからリンク情報のリストを抽出
Dim regex As Regex = New Regex("<a +href=('|"")[^('|"")]*('|"")*>")
Dim matchcollection As MatchCollection = Regex.Matches(contents)
'各リンク情報からURLを抽出し、ダウンロードする
Dim i As Integer
For i = 0 To matchcollection.Count - 1
Dim urlregex As Regex = New Regex("('|"")[^('|"")]*('|"")")
Dim match As Match = urlregex.Match(matchcollection(i).Value)
Dim url As String = match.Value.Substring(1, match.Value.Length - 2)
If url.IndexOf("http://") <> 0 Then
url = downloaddirurl & url
***************//追加部分
'抽出されたリンク情報をテキストボックスに表示
Dim fsk As FileStream = New FileStream("c:\urllist.txt", FileMode.Open)
Dim fsw As StreamWriter = New StreamWriter(fsk)
fsw.WriteLine(url)
***************
urlListText.Text = urlListText.Text & url & vbCrLf
'ファイル名を取得
Dim filenamestart As Integer = url.LastIndexOf("/") + 1
Dim filenamelength As Integer = url.Length - filenamestart
Dim filename As String = url.Substring(filenamestart, filenamelength)
'ダウンロード先ファイルパスを作成
Dim downloadpath As String = filepath & "down"
Try
webClient.DownloadFile(url, downloadpath)
Catch ex As Exception
End Try
End If
Next
Catch ex As Exception
End Try
End Sub
一週間ほど前に教えていただいて完成したソースに3行を追加したのみです。
書きこのサイトを参考にしてます。
http://www.gotdotnet.com/japan/student/column/firststep/firststep19.aspx
一週間ほど前に教えていただいて完成したソースに3行を追加したのみです。
書きこのサイトを参考にしてます。
http://www.gotdotnet.com/japan/student/column/firststep/firststep19.aspx
fsw.Closeがないからじゃ?
ありがとうございました。
無事動きました。
初心者すぎる質問ですみませんでした。
解決ボタン忘れてました。
ツイート | ![]() |