using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace xpathGet
{
class Program
{
#region webclient创建 public class WebClientBD : System.Net.WebClient
{
protected override System.Net.WebRequest GetWebRequest(Uri address)
{ HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
request.AllowAutoRedirect = false;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
return request;
} public WebResponse Response { get; private set; } protected override WebResponse GetWebResponse(WebRequest request)
{
try
{
this.Response = base.GetWebResponse(request);
}
catch { }
return this.Response; } }
public static string lastUrl(string url)
{ byte[] pageData = null; string lasturl = null;
string redirectLocal = null;
try
{ WebClientBD wc = new WebClientBD(); // 创建WebClient实例提供向URI 标识的资源发送数据和从URI 标识的资源接收数据
wc.Credentials = CredentialCache.DefaultCredentials; // 获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312 pageData = wc.DownloadData(url); // 从资源下载数据并返回字节数组。 if ((wc.Response as HttpWebResponse).StatusCode == HttpStatusCode.Found)
{
redirectLocal = ((wc.Response as HttpWebResponse).Headers["location"].StartsWith("http") == true ? string.Empty : "http://www.baidu.com") + (wc.Response as HttpWebResponse).Headers["location"];
wc = new WebClientBD(); // 创建WebClient实例提供向URI 标识的资源发送数据和从URI 标识的资源接收数据
wc.Credentials = CredentialCache.DefaultCredentials; // 获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
pageData = wc.DownloadData(redirectLocal);
if ((wc.Response as HttpWebResponse).StatusCode == HttpStatusCode.Found)
{
lasturl = (wc.Response as HttpWebResponse).Headers["location"];
}
else if ((wc.Response as HttpWebResponse).StatusCode == HttpStatusCode.OK)
{
lasturl = redirectLocal;
}
}
return lasturl; }
catch (Exception ex)
{ return "error:" + ex.Message;
}
}
#endregion
public static string GetHtmlSource(string url)
{ WebClientBD wc = new WebClientBD(); // 创建WebClient实例提供向URI 标识的资源发送数据和从URI 标识的资源接收数据
wc.Credentials = CredentialCache.DefaultCredentials; // 获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
Encoding enc = Encoding.GetEncoding("utf-8"); // 如果是乱码就改成 utf-8 / GB2312
var pageData = wc.DownloadData(url); // 从资源下载数据并返回字节数组。
return enc.GetString(pageData);
} static void Main(string[] args)
{
#region 翻页URL
//http://www.baidu.com/s?wd={0}&pn={1}&oq={0}&ie=utf-8&usm=4
#endregion
string address = "http://www.baidu.com/s?wd={0}&pn={1}0&oq={0}&ie=utf-8&usm=4";
string key = "代购";
string htmlPageSource = string.Empty; List<string> 其他最后的List = new List<string>();
List<string> 广告最后的List = new List<string>(); string 广告 = "//div[@id='content_left']//div[contains(@id,'300')]{0}|//div[@id='content_left']//div[contains(@id,'400')]{1}";
string 其他 = "//div[@id='content_left']//div[contains(@class,'c-container')]{0}";
//取标题 for (int pnIndex = 0; pnIndex < 5; pnIndex++)
{
HtmlDocument doc = new HtmlDocument();
List<string> 其他标题List = new List<string>();
List<string> 其他标题链接List = new List<string>();
List<string> 其他内容List = new List<string>();
List<string> 其他citeList = new List<string>();
List<string> 广告标题List = new List<string>();
List<string> 广告标题链接List = new List<string>();
List<string> 广告内容List = new List<string>();
List<string> 广告citeList = new List<string>();
htmlPageSource = GetHtmlSource(string.Format(address, key, pnIndex.ToString()));
doc.LoadHtml(htmlPageSource);
// doc.Load("D:\\rootInfo.html", Encoding.UTF8);
HtmlNodeCollection 广告标题 = doc.DocumentNode.SelectNodes(string.Format(广告, "/div[1]/h3/a[1]", "/div[1]/h3/a[1]"));
HtmlNodeCollection 广告标题链接 = doc.DocumentNode.SelectNodes(string.Format(广告, "/div[1]/h3/a[1]", "/div[1]/h3/a[1]"));
HtmlNodeCollection 广告内容 = doc.DocumentNode.SelectNodes(string.Format(广告, "/div[2]", "/div[2]"));
HtmlNodeCollection 广告cite = doc.DocumentNode.SelectNodes(string.Format(广告, "/div[2]//a/span[1]", "/div[3]/a/span"));
HtmlNodeCollection 其他标题 = doc.DocumentNode.SelectNodes(string.Format(其他, "/h3/a[1]"));
HtmlNodeCollection 其他标题链接 = doc.DocumentNode.SelectNodes(string.Format(其他, "/h3/a[1]"));
HtmlNodeCollection 其他内容 = doc.DocumentNode.SelectNodes(string.Format(其他, "//div[@class='c-abstract']") + "|" + string.Format(其他, "//div['c-span18 c-span-last']/p[1]") + "|" + string.Format(其他, "//div[@class='c-offset']") + "|" + string.Format(其他, "//div[@class='op_dict_content']") + "|" + string.Format(其他, "//p[contains(text(),'由于该网站的robots.txt文件存在限制指令')]"));
HtmlNodeCollection 其他cite = doc.DocumentNode.SelectNodes(string.Format(其他, "//span[@class='c-showurl']") + "|" + string.Format(其他, "//a[@class='c-showurl']"));
//分析每个结果都有一个标题,现在是最新相关信息的结果没有cite,判断哪个没有cite给其赋值“new info”
//如果有最新相关信息的结果 ,假设标题有9个结果,则cite有8个。
// 1.如果最新消息在最后一个此时标题的index=8,cite的index=8,最后一个标题index=9,但是cite的index是不存在的所以添加一个元素“new info。其他位置则插入元素 foreach (var item in 其他cite)
{
其他citeList.Add(item.InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
} foreach (var item in 其他内容)
{
其他内容List.Add(item.InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty)); } for (int i = 0; i < 其他标题.Count; i++)
{
其他标题List.Add(其他标题[i].InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
其他标题链接List.Add(其他标题链接[i].GetAttributeValue("href", "").Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
} if (其他标题List.Count != 其他citeList.Count)
{
if (其他标题[其他citeList.Count].InnerText.Contains("的最新相关信息"))
{
其他citeList.Add("new info");
}
else
{ for (int i = 0; i < 其他标题List.Count; i++)
{
if (其他标题List[i].Contains("的最新相关信息"))
{
其他citeList.Insert(i, "new info");
}
}
}
}
// List<string> 其他最后的List = new List<string>(); for (int j = 0; j < 其他标题List.Count; j++)
{
其他最后的List.Add(其他标题List[j] + "|" + 其他标题链接List[j] + "|" + 其他内容List[j] + "|" + 其他citeList[j] + "\t");
}
其他最后的List.Add(String.Format("以上为第{0}页搜索结果。", pnIndex + 1));
string path = @"d:\\infolist_Page" + (pnIndex+1) + ".html";
File.WriteAllText(path, htmlPageSource, Encoding.UTF8); for (int i = 0; i < 广告标题.Count; i++)
{
广告标题List.Add(广告标题[i].InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
广告内容List.Add(广告内容[i].InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
广告标题链接List.Add(广告标题链接[i].GetAttributeValue("href", "").Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
广告citeList.Add(广告cite[i].InnerText.Trim().Replace(" ", String.Empty).Replace("\n", string.Empty).Replace(" ", string.Empty));
}
for (int j = 0; j < 广告标题List.Count; j++)
{
广告最后的List.Add(广告标题List[j] + "|" + 广告标题链接List[j] + "|" + 广告内容List[j] + "|" + 广告citeList[j] + "\t");
}
广告最后的List.Add(String.Format("以上为第{0}页搜索结果。", pnIndex + 1));
} File.WriteAllLines(@"d:\\infolist.txt", 其他最后的List.ToArray(), Encoding.UTF8);
File.WriteAllLines(@"d:\\infolist2.txt", 广告最后的List.ToArray(), Encoding.UTF8);
}
}
}

  

C# 百度搜索结果xpath分析的更多相关文章

  1. PHP网络爬虫实践:抓取百度搜索结果,并分析数据结构

    百度的搜索引擎有反爬虫机制,我先直接用guzzle试试水.代码如下: <?php /** * Created by Benjiemin * Date: 2020/3/5 * Time: 14:5 ...

  2. python--selenium简单模拟百度搜索点击器

    python--selenium简单模拟百度搜索点击器 发布时间:2018-02-28 来源:网络 上传者:用户 关键字: selenium 模拟 简单 点击 搜索 百度 发表文章摘要:用途:简单模拟 ...

  3. Python:输入关键字进行百度搜索并爬取搜索结果

    学习自:手把手教你用Python爬取百度搜索结果并保存 - 云+社区 - 腾讯云 如何利用python模拟百度搜索,Python交流,技术交流区,鱼C论坛 指定关键字,对其进行百度搜索,保存搜索结果, ...

  4. Splinter学习--初探1,模拟百度搜索

    Splinter是以Selenium, PhantomJS 和 zope.testbrowser为基础构建的web自动化测试工具,基本原理同selenium 支持的浏览器包括:Chrome, Fire ...

  5. 利用python爬取海量疾病名称百度搜索词条目数的爬虫实现

    实验原因: 目前有一个医疗百科检索项目,该项目中对关键词进行检索后,返回的结果很多,可惜结果的排序很不好,影响用户体验.简单来说,搜索出来的所有符合疾病中,有可能是最不常见的疾病是排在第一个的,而最有 ...

  6. jsonp跨越请求百度搜索api 实现下拉列表提示

    题目来源: 最近在做百度IFE前端技术学院的题,然后有一题就是模拟百度搜索智能提示.题目是开源的,稍后给出地址. 因为博主没学过后端啊,欲哭无泪,所以不能实现后端模糊搜索,那如果前端ajax纯粹请求一 ...

  7. 百度搜索URL参数 搜索关键字

    http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词: http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜 ...

  8. python爬取百度搜索结果ur汇总

    写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...

  9. 使用curl制作简易百度搜索

    这几天研究了一下php中的curl类库,做了一个简单的百度搜索,先上代码 <div style="width:200px;height:100px;"> <div ...

随机推荐

  1. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  2. Alpha 冲刺七

    团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 无进展!!!! 项目 ...

  3. css命名管理混乱?不妨试试BEM

    css的缺陷 我们知道,css使用中一个比较令人烦恼的问题,就是css没有作用域可言,我们写了一个组件或者模块之后,往往希望它们可以四处复用,但是由于css没有作用域,我们给样式命名的时候都会非常小心 ...

  4. NOIP2018划水记

    // 6次NOIP里最爽的一次 去年的NOIP的游记:https://www.cnblogs.com/GXZlegend/p/7880740.html Day 0 特意请了一天假复习NOIP 实际上是 ...

  5. Rain on your Parade HDU - 2389 (hc板题)

    在客人能够拿到的伞与客人之间建边  跑hc就好了.... 看看别人的:https://blog.csdn.net/wall_f/article/details/8248350 #include < ...

  6. hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. 【题解】 [HNOI2004]宠物收养场(Splay)

    懒得复制,戳我戳我 Solution: \(Splay\)板子,注意交换的地方,然后就是注意不要越界node[x],应该是\(node[now]\),其次就是数组可以开大点 Code: //It is ...

  8. 自定义Kettle数据库插件

    项目需要实现使用Kettle向神通数据库中写入数据,Kettle官方标准的数据库插件里面并没有对神通数据库的支持,因此需要自己写一个数据库插件.下面我们开始写一个数据库插件 1.在eclipse中创建 ...

  9. 【转】安全加密(一):这些MCU加密方法你都知道吗?

    本文导读 随着物联网和边缘计算的出现,五花八门的MCU也被应用其中,如何保证我们的程序安全和知识产权不受侵犯呢,本文我们将对主流MCU的程序加密进行讲解,希望能够帮助你选择最适合自己应用的微处理器. ...

  10. 洛谷 P2303 [SDOi2012]Longge的问题 解题报告

    P2303 [SDOi2012]Longge的问题 题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数\(N\),你需要 ...