首页 > 解决方案 > 从网站上抓取多个列表。

问题描述

我目前正在为一个显示数据表的网站开发网络爬虫。我遇到的问题是该网站在第一次搜索时没有按状态对我的搜索进行排序。我必须在加载时通​​过第二页上的下拉菜单执行此操作。我加载第一页的方式是我认为是 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);

        }
    }

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

谢谢你花时间看这个!!!

标签: c#postparametersrequestwebclient

解决方案


我只是忘记将 cookie 添加到新搜索中。使用 google chrome 或 fiddler,您可以查看网络流量。我需要做的就是添加

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

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

@derloopkat 指出,归功于那个人!!!


推荐阅读