从网站上刮取多个列表。

时间:2018-08-13 22:33:57

标签: c# post parameters request webclient

我目前正在为显示数据表的网站制作网络抓取工具。我遇到的问题是,该网站没有在第一次搜索时按状态对我的搜索进行排序。我必须通过加载时第二页上的下拉菜单来执行此操作。我加载第一页的方式与我认为是WebClient POST请求的方式有关。我得到了正确的html响应并且可以解析它,但是我想加载更多经过过滤的搜索,但是当我将其与chrome开发人员标签中看到的html进行比较时,返回的html是不正确的。

这是我的代码

    // The website I'm looking at.
    public string url = "https://www.missingmoney.com/Main/Search.cfm";

    // The POST requests for the working search, but doesn't filter by states
    public string myPara1 = "hJava=Y&SearchFirstName=Jacob&SearchLastName=Smith&HomeState=MN&frontpage=1&GO.x=19&GO.y=18&GO=Go";
    // The POST request that also filters by state, but doesn't return the correct html that I would need to parse
    public string myPara2 = "hJava=Y&SearchLocation=1&SearchFirstName=Jacob&SearchMiddleName=&SearchLastName=Smith&SearchCity=&SearchStateID=MN&GO.x=17&GO.y=14&GO=Go";

    // I save the two html responses in these
    public string htmlResult1;
    public string htmlResult2;

    public void LoadHtml(string firstName, string lastName)
    {
        using (WebClient client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            htmlResult1 = client.UploadString(url, myPara1);
            htmlResult2 = client.UploadString(url, myPara2);

        }
    }

只是想弄清楚为什么我第一次输入参数时它起作用,而当我第二次输入参数时却不起作用。

感谢您花费的时间查看此内容!!!

1 个答案:

答案 0 :(得分:2)

我只是忘了将cookie添加到新搜索中。使用谷歌浏览器或小提琴手,您可以看到网络流量。我要做的就是添加

client.Headers.Add(HttpRequestHeader.Cookie, "cookie");

在我的代码上传之前。这样做给了我正确的html响应,现在我可以解析我的数据了。

@derloopkat指出了这一点,感谢那个人!!!