我目前正在学习c#,我对如何在c#中控制网站,c#如何与网站一起工作有疑问。
例如,假设我打开了一个空白页面的 chrome。
Process.Start("http://blank.org/"); //<- like this.
(有什么方法可以打开空白页吗?-虽然不是很重要..)
问题 1)
现在我想使用地址栏访问 YouTube。 例如,我有:
string address = "www.youtube.com"
我想在地址栏中输入这个字符串并自动点击搜索按钮。 可能吗?
问题 2)
假设我解决了问题 1。 之后,我想使用搜索栏搜索一些东西 例如,我有:
string keyword = "funny cat video";
那我想在搜索栏输入这个关键字,然后搜索。
我该怎么做? 在 C# 中可以吗?
问题 3)
然后我想在屏幕上播放第一个结果。 例如,像这样: The first result that is found with the keyword
遇到这种情况,我该怎么办?
非常感谢您。我整天都在谷歌搜索,但找不到该怎么做。
最佳答案
试试这个进行简单的搜索:
string youtubeUrl = @"https://www.youtube.com";
string keyword = "funny cat video";
string searchUrl = string.Format("{0}/results?search_query={1}", youtubeUrl, keyword.Replace(" ", "+"));
Process.Start(searchUrl);
如果你想操纵搜索结果,那么你可以使用 YouTube API获取第一个结果youtube VideoId。
如果您不想使用 YouTube Search API , 那么您将需要 download the html and parse它可以找到页面上的第一个视频链接(就像在 jquery 中一样: $("a[href^='/watch?v=']")
)。
获得所需的 youtube 视频 ID 后,您可以打开并观看它。
string youtubeUrl = @"https://www.youtube.com";
string youtubeVideoId = "njSyHmcEdkw";
string watchVideoUrl = string.Format("{0}/watch?v={1}", youtubeUrl, youtubeVideoId);
Process.Start(watchVideoUrl);
关于c# - 如何使用字符串变量来搜索网站/转到网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43927326/