Getting html data with DownloadStringCompletedEventArgs - c#

I want to get some data from a website but my code can't get url. Because if state is always false and my textbox called "weather1" always display "ece".
When I make the value of textbox as e.Result instead of "ece", it displays "DownloadStringCompletedEventArgs ".
And there is no error or warning.
What should I do? What's wrong with the code?
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
string res = Convert.ToString(e.Result);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(res);
var table = doc.DocumentNode.SelectSingleNode("//table[#class='tbl_sond']");
var degree = table.SelectSingleNode("//td[#class='renkMax']");
var date = table.SelectSingleNode("//td[#class='sond_zaman']");
}
else weather1.Text = "ece";
}
public void getWeatherInfo() {
string url = "http://www.mgm.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL";
WebClient webclient = new WebClient();
webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
Uri uri = new Uri(url);
webclient.DownloadStringAsync(uri);
}

Related

How would I grab certain information from HTML scraping, if the website returns it as not loaded?

When I write the HTML to a file, and open the file, it says "The inventory is not available" I tried using a user-agent, but I had no luck. Any help?
The result I was hoping for is to grab the entire inventory, and print out the list of a desired page on their inventory to the listbox. (Windows Forms Application, HtmlAgilityPack)
namespace projectWFA
{
public partial class Form1 : Form
{
public string selectedCurrency;
public string urlBase = "http://www.steamcommunity.com/id/";
public string text_username;
// HTMLAGILITYPACK
public HtmlWeb htmlweb;
public HtmlAgilityPack.HtmlDocument htmldocument_pageInventory;
public HtmlAgilityPack.HtmlDocument htmldocument;
public Form1()
{
InitializeComponent();
comboboxCurrency.SelectedIndex = 0;
comboboxPlaceHolder1.SelectedIndex = 0;
comboboxPlaceHolder2.SelectedIndex = 0;
// WEB STUFF
htmlweb = new HtmlWeb();
}
private void Form1_Load(object sender, EventArgs e)
{
selectedCurrency = comboboxCurrency.Items[comboboxCurrency.SelectedIndex].ToString();
Console.Out.WriteLine("selCur: " + selectedCurrency);
}
private void buttonAccept_Click(object sender, EventArgs e)
{
labelPrintUsername.Text = "Loading webpage.. Please wait..";
// LOAD WEBPAGE
htmldocument = htmlweb.Load(urlBase + textboxProfileLink.Text);
Console.Out.WriteLine("Link: " + urlBase + textboxProfileLink.Text);
if (htmldocument != null)
{
Console.Out.WriteLine("Found profile");
HtmlNode node_username = htmldocument.DocumentNode.SelectSingleNode("//span[#class='actual_persona_name']");
if (node_username != null)
{
text_username = node_username.InnerText;
labelPrintUsername.Text = text_username + "'s Inventory";
listboxItems.Items.Clear();
htmldocument_pageInventory = htmlweb.Load(urlBase + textboxProfileLink.Text + "/inventory/");
System.IO.File.WriteAllText(#"C:\Users\...\Desktop\asd.html", htmldocument_pageInventory.DocumentNode.InnerHtml);
}
}
else if (htmldocument == null)
{
Console.Out.WriteLine("Couldn't find profile");
}
}
}
}
To get the page as it would be from using a browser, use this instead:
string data = "";
using (WebClient client = new WebClient())
{
data = client.DownloadString(url);
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data));
doc.Load(stream);

C# Web Browser control only loads one page, will not work on the second attempt

I have urls in a listbox. I am trying to navigate to a url when it is selected.
private void lstURL_SelectedIndexChanged(object sender, EventArgs e)
{
wbrBrowser.Navigate(lstURL.Text);
lblUrl.Text = lstURL.Text;
lblTitle.Text = "Loading...";
System.Windows.Forms.HtmlDocument document = wbrBrowser.Document;
document.MouseUp += new HtmlElementEventHandler(this.htmlDocument_Click);
}
private void wbrBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
lblTitle.Text = wbrBrowser.Document.Title;
}
private void htmlDocument_Click(object sender, HtmlElementEventArgs e)
{
HtmlElement element = this.wbrBrowser.Document.GetElementFromPoint(e.ClientMousePosition);
var savedId = element.Id;
var uniqueId = Guid.NewGuid().ToString();
element.Id = uniqueId;
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.Document.GetElementsByTagName("html")[0].OuterHtml);
element.Id = savedId;
var node = doc.GetElementbyId(uniqueId);
var xpath = node.XPath;
lblXpath.Text = xpath;
}
It works the first time I load a page, after that it just freezes and lblTitle.Text just stays at "Loading..."
I have been searching for a while but I can't figure out why this is happening.

How to pass a value from one function to another in event handler methods in c#

I have list box populated using web-client and I used , for-loop to separate object from json response for my use ,I need to use those objects throughout the class ,i.e I need to use those values in all the methods,It will be even feasible if can Pass from one method to another
Code:
void Downloadpage()
{
WebClient webclient = new WebClient();
webclient.Headers["ContentType"] = "application/json";
webclient.DownloadStringCompleted += wc_downloadStringCompleted;
webclient.DownloadStringAsync(new Uri("http://client.web.net/pages_wp.php"), UriKind.RelativeOrAbsolute);
}
public void wc_downloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string lreport = e.Result.ToString();
string lnoHTMLs = Regex.Replace(lreport, #"<[^>]+>| |‌|»|«|“|\\n|\\t|", "", RegexOptions.Multiline).Trim();
string lnoHTMLNormaliseds = Regex.Replace(lnoHTMLs, #"\s{2,}", " ");
JArray res = JArray.Parse(lnoHTMLNormaliseds);
news = new List<jsons>();
string rId = res[0]["raportId"].ToString(); ---->a
string rTitle = res[0]["raportTitle"].ToString(); --->b
news.Add(new jsons() { raportId = rId, raportTitle = rTitle});
Presslist.ItemsSource = news;
}
I need to access values in 'a'and 'b' in another button click event as below
private void Add_to_cart(object sender, EventArgs e)
{
//values need to come here
}
Note: Add_to_cart is triggered on button click
Make the variables class level
String _rId ="";
String _rTitle ="";
void Downloadpage()
{
WebClient webclient = new WebClient();
webclient.Headers["ContentType"] = "application/json";
webclient.DownloadStringCompleted += wc_downloadStringCompleted;
webclient.DownloadStringAsync(new Uri("http://client.web.net/pages_wp.php"),
UriKind.RelativeOrAbsolute);
}
public void wc_downloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string lreport = e.Result.ToString();
string lnoHTMLs = Regex.Replace(lreport, #"<[^>]+>| |‌|»|«|“|\\n|\\t|", "", RegexOptions.Multiline).Trim();
string lnoHTMLNormaliseds = Regex.Replace(lnoHTMLs, #"\s{2,}", " ");
JArray res = JArray.Parse(lnoHTMLNormaliseds);
news = new List<jsons>();
string rId = res[0]["raportId"].ToString(); // ---->a
string rTitle = res[0]["raportTitle"].ToString(); // --->b
news.Add(new jsons() { raportId = rId, raportTitle = rTitle});
_rId = rId;
_rTitle = rTitle;
Presslist.ItemsSource = news;
}
private void Add_to_cart(object sender, EventArgs e)
{
//values need to come here
//_rId
//_rTitle
}
I think the place where you are calling your Downloadpage() method can save these values in a common place. May be Session Level or Application level space.
I think that Add_to_cart() method is a button click event so the DownloadPage should be called in page load event or any event prior to display the UI.
And when the Add_to_cart() is called than you can retrieve the values and use them.

How to retrieve a parameter from DownloadDataAsync?

Using WebClient in WPF app, the following code works fine, when an image is downloaded an event fire correctly.
I need to pass some parameters to ImageDownloadCompleted in order to specifically know which image has been just downloaded.
Using webClient.DownloadDataAsync(new Uri(url), url); I cannot get the result wanted.
What am I doing wrong here?
PS: Basically I would use this parameters to order in an array the images resulted. If very is another way to achieve this, please let me know.
private void DownloadAndPrintImagesAsync(IEnumerable<string> urls)
{
foreach (var url in urls)
{
var webClient = new WebClient();
webClient.DownloadDataCompleted += ImageDownloadCompleted;
webClient.DownloadDataAsync(new Uri(url), url); // I want to pass url
}
}
private void ImageDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
// I need to get url here
}
}
It's in the UserState property of the DownloadDataCompletedEventArgs argument:
private void ImageDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
var url = (string)e.UserState;
...
}
}

C# stopping an infinite foreach loop

This foreach loop checks a webpage and sees if there are any images then downloads them. How do i stop it? When i press the button it continues the loop forever.
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=browser_DocumentCompleted;
browser.Navigate(textBox1.Text);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
int count = 0; //if available
int maximumCount = imgCollection.Count;
try
{
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
webClient.DownloadFile(url, url.Substring(url.LastIndexOf('/')));
count++;
if(count >= maximumCount)
break;
}
}
catch { MessageBox.Show("errr"); }
}
use the break; keyword to break out of a loop
You do not have an infinite loop, you have an exception that is being thrown based on how you are writing the file to disk
private void button1_Click(object sender, EventArgs e)
{
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate("www.google.ca");
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}
That code works fine on my environment. The issue you seemed to be having was when you were setting the DownloadFile filepath, you were setting it to a value like `\myimage.png', and the webclient could not find the path so it threw and exception.
The above code drops it into the current directory with the extension name.
Maybe the Event browser.DocumentCompleted cause the error, if the page refreshes the event gets fired again. You could try to deregister the event.
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
browser.DocumentCompleted -= browser_DocumentCompleted;
HtmlElementCollection imgCollection = browser.Document.GetElementsByTagName("img");
WebClient webClient = new WebClient();
foreach (HtmlElement img in imgCollection)
{
string url = img.GetAttribute("src");
string name = System.IO.Path.GetFileName(url);
string path = System.IO.Path.Combine(Environment.CurrentDirectory, name);
webClient.DownloadFile(url, path);
}
}

Categories

Resources