c# - 从网站上抓取多个列表。
问题描述
我目前正在为一个显示数据表的网站开发网络爬虫。我遇到的问题是该网站在第一次搜索时没有按状态对我的搜索进行排序。我必须在加载时通过第二页上的下拉菜单执行此操作。我加载第一页的方式是我认为是 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);
}
}
只是想弄清楚为什么我第一次传入我的参数时它会起作用,而当我在第二个时它却不起作用。
谢谢你花时间看这个!!!
解决方案
我只是忘记将 cookie 添加到新搜索中。使用 google chrome 或 fiddler,您可以查看网络流量。我需要做的就是添加
client.Headers.Add(HttpRequestHeader.Cookie, "cookie");
在它上传之前到我的代码。这样做给了我正确的 html 响应,我现在可以解析我的数据。
@derloopkat 指出,归功于那个人!!!
推荐阅读
- reactjs - 如何使用 React-Hooks 在表单提交时选择多个复选框值并发布到服务器
- javascript - 如何检查字符串是否包含列表的任何元素并获取元素的值?
- visual-c++ - 使用适用于 Windows 客户端的接口 ivsssoftwaresnapshotprovider 还原卷影副本
- java - 如何将异步调用合并到反应式管道中
- python - SQLAlchemy scalar() 使用 SQLite 返回 DATETIME('now') 的字符串
- mysql - 如何选择每个月具有最大时间戳的行?
- r - 如何转换死亡率表以生成 Kaplan-Meier 图
- spring-boot - 在springboot中使用embeddedId内的序列
- firebase - FCM颤振启用通知振动
- javascript - 当页面重定向到另一个页面以使用 react-router-dom 做出反应时,外部 javascript 和插件不起作用