【发布时间】: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