想写个网页采集器、请问C# 自已写的winForm,如何获得浏览器(如360)网页中选取的部分内容(文本和图片)?
解决方案 »
- 批量删除
- DirectUI文本框问题
- 做了一个修改密码窗口,结果运行提示Update语法错误。
- 内存块中读数据
- 我用VS2008中的C#如何连接SQLITE数据库?有没有这方面的资料?
- 怎样 把DataGridView 中数据 [color=#FF0000][b]追加[/b][/color]到[color=#FF0000][b]Word文档[/b][/color]
- 求一自动生成流水号的C#代码!
- 想做个触摸屏的界面,B/S的
- DataGrid動態綁定列?
- 请问在VC#中如何导入*.bmp的图像资源文件,并在程序运行中将图中的某一部分在窗体的指定位置上显示出来?
- 使用ADO. NET怎么做项目
- winform 滚动条拉不到底
第一种用 Clipboard.GetDataObject(),但这种方法,不能图文一并获取,而且无法获得链接内容(即源码、htmlText)。
第二种用 EnumWindows--mshtml.IHTMLDocument2,但这种方法只能获取兼容模式内容,即InternetExplorer,无法获取极速模式的Chrorme内容?
烦请各位大神能指点一下迷津,指条明路。谢谢!
第二种,用 webbrowser 组件通过 Dom 来处理,这个更方便一些。
用 webbrowser 组件通过 Dom 来处理,我试过,好像得不到360浏览器或者qq浏览器页面内容(在极速模式下)
https://zhidao.baidu.com/question/552246331.html
窗口句柄:985154
窗口标题:Chrome Legacy Window
类名:Chrome_RenderWidgetHostHWND兼容模式:
窗口句柄:1507940
窗口标题:
类名:Internet Explorer_Server
//--------- 调用 方式为 string htm= HttpGetPageHtml("https://www.ip.cn", "utf-8");
/// <summary>
/// 获取页面html
/// </summary>
/// <param name="url">请求的地址</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public static string HttpGetPageHtml(string url, string encoding)
{
string pageHtml = string.Empty;
try
{
using (WebClient MyWebClient = new WebClient())
{
Encoding encode = Encoding.GetEncoding(encoding);
// 确定自己浏览器的User-Agent信息
// 首先打开你的浏览器输入:about:version MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER");
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
pageHtml = encode.GetString(pageData);
}
}
catch (Exception e)
{
return e.Message.ToString();
}
return pageHtml;
}
{
class Program
{
static void Main(string[] args)
{
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
httpClient.BaseAddress = new Uri("https://dms.esnadexpress.com/"); System.Net.Http.HttpRequestMessage httpRequestMessage = new System.Net.Http.HttpRequestMessage();
httpRequestMessage.Method = new System.Net.Http.HttpMethod("GET");
httpRequestMessage.Headers.Add("context-type", "application/pdf");
httpRequestMessage.RequestUri = new Uri("https://dms.esnadexpress.com/img/2019-07-16/2/custom_pdf_ESNAD_1563242468479_54981_3223fe36-31c3-4d73-b447-d2c4720046fa.pdf"); System.Net.Http.HttpResponseMessage httpResponseMessage = httpClient.SendAsync(httpRequestMessage).Result; if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
System.IO.Stream stream = httpResponseMessage.Content.ReadAsStreamAsync().Result;
using (System.IO.FileStream fs = new System.IO.FileStream("e:\\download.pdf", System.IO.FileMode.CreateNew))
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
}
}
}
}
或者使用油猴插件,写JS代码,将选区发送到指定的地址,再写入数据库或者文件里
我先解释下,为什么不用httprequest 或 WebClient:
第一,我不知道我要的资料在哪,只有通过浏览器搜寻找到想要的内容。
第二,想要的内容不一定是全文,可能只是其中的部分内容。也就是说要在网页中检索出我要的内容。
第三,如果再调用httprequest 或 WebClient把网页或图片下载下来,再分析提出,等于要检索2次。还要在winForm和浏览器中切换。效率太低,达不到自动化的效果。其次,我想说一下 我的主要问题是:如何获得浏览器(如360浏览器)里网页中选取的部分内容(文本和图片)?