为什么我不能在windows窗体中使用scrapysharp? [英] Why can't I us scrapysharp in a windows form?

查看:70
本文介绍了为什么我不能在windows窗体中使用scrapysharp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了如何在控制台应用程序中使用scrapysharp。但是当我切换到使用表单(为了更好的用户界面)时,程序会提到这行代码:



I figured out how to use scrapysharp in console application. But when I switched over to using a form (for better user interface), the program hands up on this line of code:

WebPage PageResult = Browser.NavigateToPage(new Uri("http://192.168.1.99"));





我的尝试:



以下是完整的控制台应用程序:





What I have tried:

Here is the full Console application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ScrapySharp.Core;
using ScrapySharp.Html.Parsing;
using ScrapySharp.Network;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using ScrapySharp.Html.Forms;

namespace SampleScraperClient
{
    class Program
    {
        static void Main(string[] args)
        {
            // setup the browser
            ScrapingBrowser Browser = new ScrapingBrowser();
            Browser.AllowAutoRedirect = true; // Browser has many settings you can access in setup
            Browser.AllowMetaRedirect = true;
            //go to the home page
            WebPage PageResult = Browser.NavigateToPage(new Uri("http://192.168.1.99/"));
            // get first piece of data, the page title
            HtmlNode Center = PageResult.Html.CssSelect("Center").First();
            string Bobby = Center.InnerText;
            Console.WriteLine(Bobby);
            // find a form and send back data
            //PageWebForm form = PageResult.FindFormById("dataForm");
            // assign values to the form fields

            //Console.ReadKey();
            //Console.WriteLine(Bobby);
            Console.ReadKey();
            Console.WriteLine("this far");

            string test = Bobby;
            string search = "T1";
            int foundPos = findPermutation(test, search);
            Console.WriteLine(foundPos);
            if (foundPos != -1)
            {
                Console.WriteLine(test.Substring(foundPos, search.Length));
                Console.WriteLine(" ");
                
                char[] x = { Bobby[foundPos + 5], Bobby[foundPos + 6], Bobby[foundPos + 7], Bobby[foundPos + 8] };
                string T1 = new string(x);
                Console.WriteLine(T1);
                double j;
                if (double.TryParse(T1, out j))
                {
                    j = (1.8 * j) + 32;
                    Console.WriteLine(j);
                }
                else
                {
                    Console.WriteLine("String could not be parsed.");
                }
            }
            else
            {
                Console.WriteLine("Oops!");
            }
            Console.ReadKey();
        }

        static bool matchesPermutation(string test, string search)
        {
            string remaining = search;
            for (int i = 0; i < test.Length; i++)
            {
                int pos = remaining.IndexOf(test[i]);
                if (pos == -1)
                    return false;
                else
                    remaining = remaining.Remove(pos, 1);
            }
            return true;
        }

        static int findPermutation(string test, string search)
        {
            for (int i = 0; i < test.Length - search.Length + 1; i++)
                if (matchesPermutation(test.Substring(i, search.Length), search))
                    return i;
            return -1;
        }
    }
}







这是Windows表单代码:






And here is the Windows form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ScrapySharp.Network;
using HtmlAgilityPack;
using ScrapySharp.Extensions;

namespace UGH2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // setup the browser
            ScrapingBrowser Browser = new ScrapingBrowser();
            Browser.AllowAutoRedirect = true; // Browser has many settings you can access in setup
            Browser.AllowMetaRedirect = true;
            WebPage PageResult = Browser.NavigateToPage(new Uri("http://192.168.1.99"));
            //go to the home page
            // get first piece of data, the page title
            HtmlNode Center = PageResult.Html.CssSelect("Center").First();
            string fruit = Center.InnerText;
            label1.Text = fruit;
        }
    }
}

推荐答案

有关此主题的CodeProject文章使用C#进行Webscraping [ ^ ]。
There is a CodeProject article on this subject at Webscraping with C#[^].


我唯一能看到的是两者之间的区别示例是URI中的尾部斜杠。这可能会解决问题,因为正在使用私人IP地址。



控制台:

The only thing I can see that is a difference between the two examples is the trailing slash in the URI. That may solve the problem as a private IP address is being used.

Console:
//go to the home page
WebPage PageResult = Browser.NavigateToPage(new Uri("http://192.168.1.99/"));





WinForm:



WinForm:

WebPage PageResult = Browser.NavigateToPage(new Uri
("http://192.168.1.99"));


我完全放弃了尖锐的锐利。我只是在using语句中使用了webclient()。我还使用正则表达式来查找我需要的文本。



工作得很好。
I dropped scrappy sharp altogether. I just used webclient() inside a using statement. I also used regular expressions to find the text I needed.

Worked great.


这篇关于为什么我不能在windows窗体中使用scrapysharp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆