【问题标题】:Scraping multiple lists from a website.从网站上抓取多个列表。
【发布时间】:2018-08-13 22:33:56
【问题描述】:

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

        }
    }

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

感谢您花时间看这个!!!

【问题讨论】:

  • “我返回的 html 不正确”是什么意思
  • 我返回的 html 没有我需要解析的表格
  • 我建议使用Selenium 而不是WebClient
  • @JacobLoncar,当您第一次在 Internet 浏览器中访问该页面时,您可能会收到 cookie,并且它们会附加在帖子中。如果是这种情况,并且您没有在 C# 代码中附加这些 cookie,那么您将不会得到相同的结果。在您的互联网浏览器中打开检查工具并转到网络选项卡,捕获有效的帖子并确保您使用 C# 代码发送相同的帖子。
  • @derloopkat 感谢您提供的信息,我会试一试!

标签: c# post parameters request webclient


【解决方案1】:

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

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

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

@derloopkat 指出,感谢那个人!!!

【讨论】:

    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 2017-10-16
    • 2018-05-23
    • 2013-05-21
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多