PDA

View Full Version : خواندن html



hosseinbarnamenevis
سه شنبه 08 بهمن 1398, 19:29 عصر
سلام
چطور میشه آیتمی رو از یه سایت خوندش؟
به طور مثال
یه سایت استعلام قیمته که من باید بتونم به این قیمتی که در این سایت اعلام میشه بخونم و در دیتابیس ذخیره کنم

hamidrezax1
سه شنبه 08 بهمن 1398, 22:08 عصر
سلام دوست عزیز
این کد رو یکی از دوستان توی گروه سی شارپ قرار دادن و راهنمایی ایشون هستش.البته کد زیر برای وی بی دات نت هستش


Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions


Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
textBox1.Text = "711783" ' Product ID (dkp-711783)
button1.Text = "Get"
End Sub


Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Button1.Text = "Wait..."
Button1.Enabled = False
Application.DoEvents()
Dim url = "https://www.digikala.com/product/dkp-" & TextBox1.Text
Dim page = GetPage(url)
Dim classId = "c-product__seller-price-raw js-price-value"
Dim regex = New Regex("<div class=""" + classId + """>\s*(?<price>[^\<\s]*)\s*</div>", RegexOptions.IgnoreCase)
Dim match = regex.Match(page)
Label1.Text = IIf(match.Success, match.Groups("price").Value, "Not found!")
Button1.Text = "Get"
Button1.Enabled = True
End Sub


Private Function GetPage(ByVal url As String) As String
Try
Dim request = DirectCast(WebRequest.Create(url), HttpWebRequest)
With request
.CookieContainer = New CookieContainer
.Proxy = WebRequest.DefaultWebProxy
.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
.ProtocolVersion = HttpVersion.Version10
.Accept = "*/*"
.ContentType = "text/html"
.ContentLength = 0
End With
Using response = DirectCast(request.GetResponse(), HttpWebResponse)
Dim stream = response.GetResponseStream()
If stream Is Nothing Then
Return String.Empty
End If
Using reader = New StreamReader(stream)
Return reader.ReadToEnd
End Using
End Using
Catch
Return ""
End Try
End Function


End Class





151305
یک لیبل و یک تکست باکس و یک باتن قرار بدین در فرمتون
موفق باشید

hosseinbarnamenevis
دوشنبه 21 بهمن 1398, 13:39 عصر
ممنون میشم لینکش رو بزارید تا راهنمایی های مرتبط رو بخونم