当前位置: 首页>>代码示例>>C#>>正文


C# HtmlDocument.GetElementById方法代码示例

本文整理汇总了C#中HtmlAgilityPack.HtmlDocument.GetElementById方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlDocument.GetElementById方法的具体用法?C# HtmlDocument.GetElementById怎么用?C# HtmlDocument.GetElementById使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HtmlAgilityPack.HtmlDocument的用法示例。


在下文中一共展示了HtmlDocument.GetElementById方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetRentals

		public async Task<Rental[]> GetRentals (int page)
		{
			bool needsAuth = false;
			var rentalsUrl = ProntoRentalsUrl;
			if (page > 0)
				rentalsUrl += "/" + (page * 20);

			for (int i = 0; i < 4; i++) {
				try {
					if (needsAuth) {
						var content = new FormUrlEncodedContent (new Dictionary<string, string> {
							{ "username", credentials.Username },
							{ "password", credentials.Password }
						});
						var login = await Client.PostAsync (ProntoLoginUrl, content).ConfigureAwait (false);
						if (login.StatusCode == HttpStatusCode.Found || login.StatusCode == HttpStatusCode.OK)
							needsAuth = false;
						else
							continue;
					}

					var answer = await Client.GetStringAsync (rentalsUrl).ConfigureAwait (false);

					var doc = new HtmlDocument ();
					doc.LoadHtml (answer);
					var div = doc.GetElementById ("content");
					var table = div.Element ("table");
					return table.Element ("tbody").Elements ("tr").Select (row => {
						var items = row.Elements ("td").ToArray ();
						return new Rental {
							Id = long.Parse (items[0].InnerText.Trim ()),
							FromStationName = items [1].InnerText.Trim (),
							ToStationName = items [3].InnerText.Trim (),
							Duration = ParseRentalDuration (items [5].InnerText.Trim ()),
							Price = ParseRentalPrice (items [6].InnerText.Trim ()),
							DepartureTime = DateTime.Parse(items[2].InnerText,
							                               System.Globalization.CultureInfo.InvariantCulture),
							ArrivalTime = DateTime.Parse(items[4].InnerText,
							                             System.Globalization.CultureInfo.InvariantCulture)
						};
					}).ToArray ();
				} catch (HttpRequestException htmlException) {
					// Super hacky but oh well
					if (!needsAuth)
						needsAuth = htmlException.Message.Contains ("302");
					continue;
				} catch (Exception e) {
					e.Data ["method"] = "GetRentals";
					Xamarin.Insights.Report (e);
					Log.Error ("RentalsGenericError", e.ToString ());
					break;
				}
			}

			return null;
		}
开发者ID:SpiderMaster,项目名称:BikeNow,代码行数:56,代码来源:ProntoRentals.cs

示例2: OpenHtmlPageAsync

        public static async Task<HtmlDocument> OpenHtmlPageAsync(string url, PortableWebClient client)
        {
            var doc = new HtmlDocument();
            LOAD_PAGE:
            doc.LoadHtml(await client.DownloadStringAsync(url));
            string title = GetTitle(doc);
            // 处理重定向。
            if (title.ToLowerInvariant().Contains("object moved"))
            {
                var nextUrl = doc.DocumentNode.Descendants("a").FirstOrDefault()?.GetAttributeValue("href", "");
                if (!string.IsNullOrEmpty(nextUrl))
                {
                    url = nextUrl;
                    goto LOAD_PAGE;
                }
                throw new UnexpectedHtmlException(url, "无法解析重定向目标。");
            }
            // 有时 CAS 中间会多一步。
            if (title.Contains("统一身份认证网关"))
            {
                var loginForm = doc.GetElementById("fm1");
                if (loginForm?.Name != "form") loginForm = null;
                if (loginForm == null)
                {
                    /*
<div id="content" class="fl-screenNavigator-scroll-container"  >
<div class="info"><p>单击 <a href="http://my.xjtu.edu.cn/Login?ticket=....">这里</a> ,便能够访问到目标应用。</p></div>
</div>
                    */
                    var node = doc.GetElementById("content");
                    var nextUrl = node?.Descendants("a").FirstOrDefault()?.GetAttributeValue("href", "");
                    if (!string.IsNullOrEmpty(nextUrl))
                    {
                        url = nextUrl;
                        goto LOAD_PAGE;
                    }
                    throw new UnexpectedHtmlException(url, "无法解析重定向目标。");
                }
            }
            return doc;
        }
开发者ID:CXuesong,项目名称:App2,代码行数:41,代码来源:XjtuUtility.cs

示例3: RutorSearch

 //private static readonly Regex TableRegex = new Regex("<table.?*</table>", RegexOptions.Singleline);
 private static List<TrackerTopic> RutorSearch(Film film)
 {
     Uri uri = new Uri("http://rutor.org/search/0/0/300/2/" + film.InfoTable["name"][0] + " BDRemux");
     var htmlCode = Tools.GetURIContent(uri);
     var htmlDoc = new HtmlDocument();
     htmlDoc.LoadHtml(htmlCode);
     var results = htmlDoc.GetElementById("index");
     if (results.InnerText.IndexOf("Результатов поиска") == -1) return null;
     Regex searchResCount = new Regex(@"Результатов поиска \d+");
     var ResCount = searchResCount.Match(results.InnerText);
     if (!ResCount.Success) return null;
     int resultsCount = Convert.ToInt32(ResCount.Value.Substring(ResCount.Value.IndexOf("а ") + 2));
     if (resultsCount == 0)
         return new List<TrackerTopic>();
     //main part
     var magnet_tmp = "";
     var scr_tmp = "";
     var tor_file = "";
     var name_tmp = "";
     var size_tmp = "";
     int found = 0;
     var row = 1;
     List<TrackerTopic> TorFound = new List<TrackerTopic>();
     var table = results.Element("table");
     List<HtmlNode> x = table.Elements("tr").ToList();
     foreach (HtmlNode node in x)
     {
         if (node.Attributes.Contains("class") && node.Attributes["class"].Value == "backgr" ||
             row == 5)
             continue;
         List<HtmlNode> s = node.Elements("td").ToList();
         foreach (HtmlNode item in s)
         {
             switch (row)
             {
                 case 2:
                 {
                     var hrefs = item.Descendants("a").Where(t => t.Attributes.Contains("href")).ToList();
                     magnet_tmp = hrefs[0].Attributes["href"].Value;
                     scr_tmp = "http://rutor.org" + hrefs[1].Attributes["href"].Value;
                     name_tmp = item.InnerText.Replace("&nbsp;", " ").Trim();
                     break;
                 }
                 case 4:
                 case 3: //if there is no comments
                 {
                     Match szMatch = sz.Match(item.InnerText.Replace("&nbsp;", " "));
                     if (szMatch.Success)
                         size_tmp = item.InnerText.Replace("&nbsp;", " ");
                     break;
                 }
             }
             Console.WriteLine(item.InnerText.Replace("&nbsp;", " ")); //DEBUG
             row++;
         }
         if (IsCorrectFilmTorrent(name_tmp, film))
         {
             Regex rutogFilmIdRegex = new Regex(@"/torrent/[\d]+/");
             var rutorFilmIdMatch = rutogFilmIdRegex.Match(scr_tmp);
             var rutorFilmId = "";
             if (rutorFilmIdMatch.Success)
             {
                 rutorFilmId = rutorFilmIdMatch.Value.Substring("/torrent/".Length,
                     rutorFilmIdMatch.Value.Length - "/torrent/".Length - 1);
                 tor_file = "http://d.rutor.org/download/" + rutorFilmId;
                 TorFound.Add(new TrackerTopic(name_tmp, scr_tmp, tor_file, size_tmp, magnet_tmp));
                 found++;
                 scr_tmp = name_tmp = size_tmp = tor_file = magnet_tmp = "";
             }
         }
         row = 1;
         if (found >= Settings.MAX_SEARCH_RESULT)
             break;
         Console.WriteLine("-------"); //DEBUG
     }
     return TorFound;
 }
开发者ID:PablusVinii,项目名称:FilmTracker,代码行数:78,代码来源:TrackerSearch.cs

示例4: LoginAttemptAsync

        private async Task LoginAttemptAsync(string userName, string password)
        {
            /*
username:.....
password:.....
code:
lt:LT-.....
execution:e3s1
_eventId:submit
submit:登录
*/
            if (casAuthenticationDictBuffer == null) await UpdateCoreAsync();
            var dict = casAuthenticationDictBuffer;
            casAuthenticationDictBuffer = null;
            Debug.Assert(dict != null);
            dict["username"] = userName;
            dict["password"] = password;
            var doc = new HtmlDocument();
            using (var resp = await Client.UploadAsync(casAuthenticationUrl, dict))
                doc.LoadHtml(await resp.Content.ReadAsStringAsync());
            var node = doc.GetElementById("msg");
            if (node != null) throw new OperationFailedException(HtmlEntity.DeEntitize(node.InnerText));
            await UpdateCoreAsync();
        }
开发者ID:CXuesong,项目名称:App2,代码行数:24,代码来源:AccountManager.cs

示例5: ParseInfoTable

 private static void ParseInfoTable(string htmlCode, Film film)
 {
     var htmlDoc = new HtmlDocument();
     htmlDoc.LoadHtml(htmlCode);
     var names = htmlDoc.GetElementById("headerFilm");
     if (names == null) return;
     var _names = names.Descendants()
         .Where(t => t.Attributes.Contains("itemprop")).ToArray();
     film.InfoTable.Add("name",
         new List<string>()
         {
             MakeValueClear(_names[0].InnerText),
             MakeValueClear(_names[1].InnerText)
         });
     SetPosterInfo(film);
     var table = htmlDoc.GetElementById("infoTable");
     ClearHTMLCode(table);
     table = table.Element("table");
     string tmp = "";
     bool key = true;
     string cur_key = "";
     List<HtmlNode> x = table.Elements("tr").ToList();
     foreach (HtmlNode node in x)
     {
         List<HtmlNode> s = node.Elements("td").ToList();
         foreach (HtmlNode item in s)
         {
             tmp = MakeValueClear(item.InnerText);
             if (!String.IsNullOrWhiteSpace(tmp))
             {
                 if (key && Constants.KeyWords.ContainsKey(tmp))
                 {
                     film.InfoTable.Add(Constants.KeyWords[tmp], new List<string>());
                     cur_key = tmp;
                     key = false;
                 }
                 else
                 {
                     if (!key)
                     {
                         if (cur_key != "слоган")
                         {
                             var Arr_tmp = tmp.Split(',');
                             foreach (var i in Arr_tmp)
                             {
                                 film.InfoTable[Constants.KeyWords[cur_key]].Add(i.Trim());
                             }
                         }
                         else
                             film.InfoTable[Constants.KeyWords[cur_key]].Add(tmp);
                     }
                 }
             }
             Console.WriteLine(tmp); //DEBUG
         }
         Console.WriteLine("-------"); //DEBUG
         key = true;
     }
     //Console.ReadKey();
 }
开发者ID:PablusVinii,项目名称:FilmTracker,代码行数:60,代码来源:KinopoiskSearch.cs


注:本文中的HtmlAgilityPack.HtmlDocument.GetElementById方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。