-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserLink.cs
More file actions
65 lines (58 loc) · 2.02 KB
/
ParserLink.cs
File metadata and controls
65 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using AngleSharp.Dom;
using AngleSharp.Html.Parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace TelegramBot
{
internal class ParserLink
{
public static List<string> AngleSharp(string urlAddress)
{
List<string> hrefTags = new List<string>();
var Html = GetCode(urlAddress);
var parser = new HtmlParser();
var document = parser.ParseDocument(Html);
var elements = document.QuerySelectorAll("img");//.Where(x => x.ClassName != null && x.ClassName.Contains("teaser__img"));
foreach (IElement element in elements)
{
hrefTags.Add(element.GetAttribute("src"));
}
return hrefTags;
}
public static String GetCode(string urlAddress)
{
string data = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
Cookie cookie = new Cookie
{
Name = "beget",
Value = "begetok"
};
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Uri(urlAddress), cookie);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
return data;
}
}
}