如何使用现有网站的现有搜索文本框并获取结果? [英] How do i use an existing search textbox of an existing website and get the results?
问题描述
大家好.
我想从具有搜索框和按钮的网站上获得一些结果,我该怎么做?
这是一个使用PHP的新闻文章网站,我想搜索一个值并找回标题并在我的c#Windows应用程序中使用它们.
网站地址中没有参数和值.
我已经尝试了一些方法,但是我真的不知道该怎么做.
我只想说我是为个人使用而做的,我只想每周一次,但要以一种自动化的方式来搜索一个值,以这种方式,当我得到我感兴趣的结果时,我会让我的应用程序向我发送电子邮件.
到目前为止,我从网站上得到的是:
Hi guys..
I would like to get some results from a website that has a search box and a button, how could i do that?
It''s a news articles website in PHP and i would like to search for a value and get the titles back and use them in my c# windows application.
there are no parameters and values in the address of the website.
i have tried some things but i really don''t know how to do it..
let me just mention that i am doing it for my personal use and i only want to search for one value one time a week but in an automated way and in that way when I''ll have the results that i am interested of i would make my app send me an e-mail.
so far what i got from the website is this:
<form method="post" action="findnews.php">
<input align="left" size="32" name="search"/>
<input type="submit" value="new search" style="width: 80px;"/>
</form>
我该如何为搜索赋予自己的价值,并以文本或其他形式获得结果?
我是网络编码方面的新手..
我将不胜感激..
谢谢!
罗伊.
修订:
多亏阿比舍克的建议,我才开始努力.
虽然看起来还是有问题.
同时,我正在获取页面本身的源代码,而不是结果.
问题可能在这行:
how could i put my own value for the search and get the results as text or something?
I am kind of a Newbie as for web coding..
I will appreciate any help..
Thank you!
Roy.
Revision:
Thanks to Abishek''s advice i got someting going..
though it still seems as i have a problem.
meanwhile i am getting the source code for the page itself and not the results.
the problem may be in this line:
myWebClient.Headers.Add("search", "application/x-www-form-urlencoded");
我试图定义此行来处理页面上的搜索框,但是我不确定这是否是正确的放置位置..
我不太确定如何配置这条线,并且在MS网站上找不到任何材料来解决它..
现在的问题是:如何使用搜索框?
如果您能看一下,我将不胜感激.
这是我的C#win应用程序.
I have tried to define this line to handle the search box on the page but i am not sure if this is the correct place to do it..
I am not really sure how to configure this line and couldn''t find any material on MS sites for figuring it out..
the question now is: How do i make use of the search box?
I will appreciate if you''ll have a look..
This is my C# win application..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WinFindNews
{
public partial class Form1 : Form
{
public WebHeaderCollection Headers { get; set; }
public Form1()
{
InitializeComponent();
string uriString;
uriString = "http://thedomain.com/findnews.php";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
string postData = "test query";
//myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myWebClient.Headers.Add("search", "application/x-www-form-urlencoded");
// Display the headers in the request
richTextBox1.AppendText("Resulting Request Headers: ");
richTextBox1.AppendText(myWebClient.Headers.ToString());
// Apply ASCII Encoding to obtain the string as a byte array.
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
//Console.WriteLine("Uploading to {0} ...", uriString);
// Upload the input string using the HTTP 1.0 POST method.
byte[] responseArray = myWebClient.UploadData(uriString, "POST", byteArray);
// Decode and display the response.
//Console.WriteLine("\nResponse received was {0}",
//Encoding.ASCII.GetString(responseArray));
richTextBox1.AppendText(Encoding.ASCII.GetString(responseArray));
}
}
}
谢谢!!!
Roy.
Thank you!!!
Roy.
推荐答案
在我看来,PHP正在处理搜索.如果它不支持任何RSS/atom/web服务.使用搜索"字段&在findnews.php上执行http POST请求.它的价值.查看WebRequest&的示例WebClient.因此,您将知道如何实现这一目标.收到响应后,就可以使用Regex来获取结果.
结帐 http://msdn.microsoft.com/en-us/library/system .net.webclient.headers.aspx [ ^ ]
To me it looks like PHP is handling the search. If it does not support any RSS/atom/web services. Do a http POST request on the findnews.php with "search" field & a value to it. Check out examples of WebRequest & WebClient. So you will know how to achive this. Once you get a response, you use Regex to grab the results.
Checkout http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx[^] for example
非常感谢!我会尝试一下,看看它会把我引向何方.
谢谢!!!!
罗伊.
我已经做了一些工作,但是仍然有问题,请再次查看该问题.
谢谢!!!
Roy.
Thanks a lot! i will try that and see where it leads me..
Thanks!!!!
Roy.
I have made something work but still have proplem, please review the question again..
Thanks!!!
Roy.
如果模拟一个Web请求,您将得到一个Web响应,该响应不是PHP代码,而是HTML. Web服务器假定您是Web浏览器,因此它将发送HTML.您将需要解析它.
好的-在我看来,您的要求没有正确建立.如果您向我们展示了您得到的答复,这可能会有所帮助,但是我认为您需要正确地构建您的帖子数据.我不确定它采用什么格式,但我怀疑它的变量名=值,其中变量名是搜索框的名称.
抱歉,我刚刚在帖子开头发现了:mad:图标,我不是故意要把它放在那儿.抱歉给您留下任何错误的印象.
If you simulate a web request, you will get a web response, which will not be PHP code, but it WILL Be HTML. The web server assumes you are a web browser, and so it sends HTML. You will need to parse it.
OK - it doesn''t look to me like you''re building your request properly. It would perhaps help if you showed us the response you''re getting, but I think you need to build your post data correctly. I am not sure what format it takes, but I suspect it''s variablename=value, where variablename is the name of the search box.
I am sorry, I just spotted the :mad: icon at the start of my post, I didn''t mean to put that there. Sorry for any wrong impression.
这篇关于如何使用现有网站的现有搜索文本框并获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!