2017-02-06 3 views
-1

그래서 개인 트레이닝을 위해 게임에 대한 최고 점수 조회를 작성하려고합니다. 내 현재 코드입니다 : 나는 기술의 정수를 얻기 위해 노력하고C# 웹 응답에서 텍스트의 특정 부분 가져 오기

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://services.runescape.com/m=hiscore/a=13/compare?user1=" + textBox1.Text); 
httpWebRequest.Host = "services.runescape.com"; 
httpWebRequest.Method = "POST"; 
httpWebRequest.CookieContainer = cookie; 
httpWebRequest.Referer = "Referer: http://services.runescape.com/m=hiscore/a=13/ranking"; 
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 
httpWebRequest.ContentType = "application/x-www-form-urlencoded"; 
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"; 
httpWebRequest.ContentLength = (long)bytes.Length; 
httpWebRequest.Headers.Add("Origin", "services.runescape.com"); 
httpWebRequest.Headers.Add("Cache-Control", "max-age=0"); 
httpWebRequest.Headers.Add("Upgrade-Insecure-Requests", "1"); 

Stream requestStream = httpWebRequest.GetRequestStream(); 
requestStream.Write(bytes, 0, bytes.Length); 
requestStream.Close(); 

HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 
cookie.Add(httpWebResponse.Cookies); 

StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); 
string text = streamReader.ReadToEnd(); 
streamReader.Close(); 
httpWebResponse.Close(); 

. 기술 수준을 보유하고있는 응답 라인은 다음과 같습니다 :

<td class="playerWinLeft alignleft"><div class="relative">a href="services.runescape.com/m=hiscore/a=13/ranking?category_type=0&amp;table=0&amp;page=1">2,715/a></div></td> 

2715 내가 기록 할 부분이다.

답변

1

웹 페이지가 유효한 XML 인 경우 (항상 그런 것은 아닙니다) load it into an XML documentusing XPath이 필요한 데이터를 찾을 수 있습니다.

유효하지 않은 XML 인 경우 HTML Agility Pack과 같은 HTML 파서에 HTML을로드해야합니다.

또는 저렴한 솔루션으로 services.runescape.com/m=hiscore/a=13/ranking?category_type=0&amp;table=0&amp;page=1">의 문자열을 검색하고 바로 다음 문자 (예 : IndexOfSubstring)를 사용할 수 있습니다.

+2

HTML 민첩성 팩만 사용하십시오. 유효한 XML을 따르기 위해 웹 페이지에 의존하는 것은 끝이없는 두통에 대한 확실한 방법입니다. – Tim

+0

나는 이것을 어떻게 사용 했는가? 예가 있습니까? – MrMime

+0

필요에 따라 [이 솔루션] (http://stackoverflow.com/questions/42076207/c-sharp-htmlagilitypack-get-content-from-all-div-with-given-class)을 사용자의 요구에 맞게 조정할 수 있습니다. –

-1

Jquery Ajax를 수행하고 필요한 경우 데이터를 필터링 할 수 있습니다.

$(function() { 
      $.ajax({ 
       type:"GET", 
       url:"somePage.html", 
       dataType:"html", 
       success:function(data) { 
        alert(data); 

       }, 
       error:function() { 
        alert("Error"); 
       } 
      }) 
     }); 
+0

제외하고 질문은 C#에서 Javascript가 아니라 이것을 수행하는 것입니다 ... – Adrian

0

는 html로하는 Linq에의 당신에게

을 도울 수있는 패킷있다. XML에 Linq와 비슷합니다. 이것이 당신을 도울 수 있다고 생각합니다.

https://bitlush.com/linq-to-html

관련 문제