掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
テキストファイルに保存するには? (ID:125996)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
環境は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
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.