使用C#selenium进行简单的抓取/数据收集 [英] Simple scraping / data collecting with C# selenium not working
问题描述
因此我尝试使用C#与Selenium收集一些数据并且我对它很新,至少收集数据部分,我试图实现的是,当我按下按钮时收集一些数据并将其放入文本框中,简单,只有当我运行我的代码它不收集任何东西
我使用标签作为按钮
私人 void label11_Click( object sender,EventArgs e)
{
var getTopFive = 新 FirefoxDriver();
getTopFive.Navigate()。GoToUrl( http://www.rottentomatoes.com/top/bestofrt / top_100_action__adventure_movies /跨度>);
foreach ( var item in getTopFive.FindElements(By.TagName( a)))
// 这是我离开的地方
{
activeTextBox.Text =(item.GetAttribute( class));
}
我觉得我把它与FindElements& GetAttribute但我不确定
我尝试过:
我我们试过搞乱FindElements& GetAttribute但没有运气
for(int i = 0; i< 5; ++ i)
{
activeTextBox.Text + = movies [i] .Text +\\\\ n;
}
我看了一下页面。有一些< a class =......>
主播。
那又怎样?你没有检索任何东西。典型的锚看起来像< a class =unstyled articleLink...>
,因此您将检索字符串unstyled articleLink。任何一点? :-)
但你真的没有那么做。请注意,您将一个字符串分配给activeTextBox.Text
...一次又一次...循环... :-)
每次丢弃先前的属性值,然后写入新的属性值。 :-)
询问做什么没有任何意义。除了应用一些基本思想之外没什么可以帮助。
顺便说一下,没有必要使用任何第三方软件来完成这么简单的任务。您甚至不需要真正的HTML浏览器,因为它可以通过原始文本搜索完成,并且可以使用HttpWebRequest
类轻松获取HTML字符串,甚至更简单,基本HttpClient
...所有人都需要快速查看文档,然后关注它。
-SA
So im trying to collect some data with Selenium using C# and I am fairly new to it, atleast the collecting data part, what im trying to achieve is, when I press a button is collects some data and puts it in a textbox, simple as that, the only thing is when I run my code it doesnt collect anything
im using a label as a button
private void label11_Click(object sender, EventArgs e)
{
var getTopFive = new FirefoxDriver();
getTopFive.Navigate().GoToUrl("http://www.rottentomatoes.com/top/bestofrt/top_100_action__adventure_movies/");
foreach (var item in getTopFive.FindElements(By.TagName("a")))
//this is where I left off
{
activeTextBox.Text = (item.GetAttribute("class"));
}
I think I messed it with the FindElements & GetAttribute but im not sure
What I have tried:
I've tried messing with the FindElements & GetAttribute but no luck
for (int i = 0; i < 5; ++i)
{
activeTextBox.Text += movies[i].Text + "\r\n";
}
I looked at the page. There is a number of<a class="…" … >
anchors.
So what? You are not retrieving anything. A typical anchor looks like<a class="unstyled articleLink" … >
, so you would "retrieve" the string "unstyled articleLink". Any point? :-)
But you don't really do even that. Note that you assign a string toactiveTextBox.Text
… again and again… in a loop… :-)
Each time, the previous property value is discarded, and then new one is written. :-)
Asking "what to do" would not make any sense. Nothing except applying some elementary thinking can help.
By the way, there is no a serious need to use any 3rd-party software for such a simple task. You don't even need a real HTML browser, as it can be done with primitive text search, and the HTML string can be easily obtained withHttpWebRequest
class, even simpler, with rudimentaryHttpClient
… All one needs is quick look at the documentation, and following it.
—SA
这篇关于使用C#selenium进行简单的抓取/数据收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!