简体   繁体   English

C#从两个不同的网站获取价值

[英]C# Get Values from two different websites

I am using HTMLElementCollection , HtmlElement to iterate through a website and using Get/Set attributes of a website HTML and returning it to a ListView . 我正在使用HTMLElementCollectionHtmlElement来遍历网站,并使用网站HTML的Get / Set属性并将其返回到ListView Is it possible to get values from website a and website b to return it to the ListView ? 是否可以从网站a和网站b获取值以将其返回到ListView

HtmlElementCollection oCol1 = oDoc.Body.GetElementsByTagName("input");
foreach (HtmlElement oElement in oCol1)
{
    if (oElement.GetAttribute("id").ToString() == "search")
    {
        oElement.SetAttribute("value", m_sPartNbr);
    }
    if (oElement.GetAttribute("id").ToString() == "submit")
    {
        oElement.InvokeMember("click");
    }
}

HtmlElementCollection oCol1 = oDoc.Body.GetElementsByTagName("tr");
foreach (HtmlElement oElement1 in oCol1)
{
    if (oElement1.GetAttribute("data-mpn").ToString() == m_sPartNbr.ToUpper())
    {
        HtmlElementCollection oCol2 = oElement1.GetElementsByTagName("td");
        foreach (HtmlElement oElement2 in oCol2)
        {
            if (oElement2 != null)
            {
                if (oElement2.InnerText != null)
                {
                    if (oElement2.InnerText.StartsWith("$"))
                    {
                        string sPrice = oElement2.InnerText.Replace("$", "").Trim();
                        double dblPrice = double.Parse(sPrice);
                        if (dblPrice > 0)
                            m_dblPrices.Add(dblPrice);
                    }
                }
            }
        }
    }
}

As one of the comments mentioned the better approach would be to use HttpWebRequest to send a get request to www.bestbuy.com or whatever site. 正如提到的评论之一,更好的方法是使用HttpWebRequest将获取请求发送到www.bestbuy.com或任何站点。 What it returns is the full HTML code (what you see) which you can then parse through. 它返回的是完整的HTML代码(您看到的内容),然后可以解析该代码。 This kind of approach keeps you from seinding too many requests and getting blacklisted. 这种方法可以防止您发现过多的请求并将其列入黑名单。 If you need to click a button or type in a text field its best to mimic human input to avoid being blacklisted also. 如果您需要单击按钮或在文本字段中键入内容,则最好模仿人为输入,以避免也被列入黑名单。 I would suggest injecting a simple javascript into the page header or body and execute it from the app to send a 'onClick' event from the button (which would then reply with a new page to parse or display) or to modify the text property of something. 我建议将一个简单的javascript注入页面标题或正文中,然后从应用程序中执行它,以从按钮发送“ onClick”事件(然后该事件将以新页面答复以进行解析或显示)或修改的text属性。一些东西。

this example is in c++/cx but it originally came from ac# example. 此示例在c ++ / cx中,但其最初来自ac#示例。 the script sets the username and password text fields then clicks the login button: 该脚本设置用户名和密码文本字段,然后单击登录按钮:

    String^ script = "document.GetElementById('username-text').value='myUserName';document.getElementById('password-txt').value='myPassword';document.getElementById('btn-go').click();";
auto args = ref new Platform::Collections::Vector<Platform::String^>();
args->Append(script);
create_task(wv->InvokeScriptAsync("eval", args)).then([this](Platform::String^ response){
    //LOGIN COMPLETE
});

//notes: wv = webview //注意:wv = webview

EDIT: as pointed out the absolute best approach would be to get/request an api. 编辑:正如指出的那样,绝对最佳的方法是获取/请求一个api。 I was surprised to see that site mason pointed out for bestbuy developers. 我很惊讶地看到该网站梅森为百思买开发人员指出了这一点。 Personally I have only tried to work with auto part stores who either laugh while saying I can't afford it or have no idea what I'm asking for and hang up (when calling corporate). 就我个人而言,我只尝试与汽车零件商店合作,他们要么笑着说我买不起,要么不知道我要什么并挂断电话(打电话给公司时)。

EDIT 2: in my code the site used was autozone. 编辑2:在我的代码中使用的站点是自动区域。 I had to use chrome developer tools (f12) to get the names of the username, password, and button name. 我必须使用chrome开发人员工具(f12)来获取用户名,密码和按钮名称。 From the developer tools you can also watch what is sent from your computer to the site/server. 通过开发人员工具,您还可以查看从计算机发送到站点/服务器的内容。 This allows you to recreate everything and mimic javascript input and actions using post/get with HttpWebRequest. 这使您可以重新创建一切,并通过HttpWebRequest使用post / get模仿javascript输入和操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 LINQ 从 C# 中的数据库中获取两种不同类型的值? - How do I get two different type of values from database in C# using LINQ? 尝试通过c#中的多线程从两个不同的数据库表中获取值 - Trying to get values from two different table of database by multithreading in c# 获取 C# 列表中两个不同值组合的最高频率 - Get highest frequency of combination of two different values in a list in C# 通过比较 C# 中的两个对象来获取不同的值 - To Get Different values by comparing two objects in C# 两个人如何从2个不同的表中获取数据C# - how two get data from 2 different table c# 从C#文件中获取不同节点值的快速方法? - Quick way to get different node values from a file in C#? 如何从C#中的字符串中获取两个数值 - How to get two numerical values from a string in C# C# 如何从数据表中按 ID 获取两列值 - C# how to get two column values by ID from a datatable Visual C# - 从文本文件中读取并在包含不同值的两个数组中分隔值 - Visual C# - Reading from a text file and separating values in two arrays that hold different values 从C#中的图像获取像素值? 为什么它们与Matlab中的值不同? - Get pixel values from an image in C#? Why are they different from values in Matlab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM