zoukankan      html  css  js  c++  java
  • 当从网页获取不到元素时,

    1,可以分段解析

    2,

    using Ivony.Html.Parser;
    using Ivony.Html;
    using Skay.WebBot;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;
    using System.Data.SqlClient;
    
    namespace catchdatafirst
    {
        class Program
        {
            public static Thread th;
            static void Main(string[] args)
            {
            //本程序可能会有点关于线程运行时间混乱的情况导致错误,一般一个线程结束才开始下一个
    for(int i = 1; i <= 2; i++) { string str = "http://www.htluxe.com/category.php?id=83&price_min=0&price_max=0&page="+i+"&sort=sort_order%20asc,last_update&order=DESC"; //原网址 th = new Thread(new ParameterizedThreadStart(GetJDData)); //当使用有参数的线程时,参数在开始的时候传递 th.Start(str); } Console.ReadKey(); //防止程序自行结束,意思就是读入任意键。然后程序结束 } private static void GetJDData(Object s)//必须用object形式传入 { string str = s as string; //转换为字符串 HttpUtility http = new HttpUtility(); //应该是写好的包,直接引用的 string html = http.GetHtmlText(str); //从网页上获取源代码 var documenthtml = new JumonyParser().Parse(html); //解析 var items = documenthtml.Find(".piclist li");//查询元素 foreach(var item in items) //遍历要寻找的元素,注意foreach { string title = item.FindFirst(".base a").InnerText(); string goodsurl = "http://www.htluxe.com/"+item.FindFirst(".base a").Attribute("href").Value();
              //上两行元素
    string subhtml = http.GetHtmlText(goodsurl, "utf-8", "text/html; charset=utf-8");
              //通过utf-8形式将源代码转换相应格式?
    string Area_Html = http.GetHtmlText(goodsurl.Split('?')[0] + "?act=price&" + goodsurl.Split('?')[1], "utf-8", "text/html;charset=utf-8", "");
              //获取网页上的信息,此url发现由下图展示
    try { JObject Area_Jo = (JObject)JsonConvert.DeserializeObject(Area_Html);//还要再转换……不知道什么鬼            string priceto = Area_Jo["result"].ToString();//获得result的值在本网页上就是价格 Console.WriteLine(title + " 价格为" + " --->" + priceto); bufff(title, priceto);//插入数据库 } catch { Console.WriteLine(title + " 地址获取异常");//获取异常机制 } } } private static void bufff(string title, string priceto) { SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=StuTinafirst;User ID=sa;Password=123456"); conn.Open(); string strstr = string.Format("insert into catchdatafirst (name, price) values ('" + title + "' , '" + priceto + "')"); SqlCommand com = new SqlCommand(strstr, conn); int i = com.ExecuteNonQuery(); //获得插入之后影响的值? } } }

    在network中查看还需再点一下F5,然后找到一个类似与如下界面

    headers中可以看到url,设法拼接成一样的,然后就可以通过此网址发现上边的元素

    应该还有一种方法,还会更新……

  • 相关阅读:
    ghost版本xp安装iis的问题
    装vs2005 sp1出现数字签名错误的解决办法
    FLEX对整个截屏或单个UI进行截图
    在Apache环境下成功的运行ASP.NET
    向园子的高手们求救 关于45斜视地图的寻路
    第一次在博客里面写东西
    在 Linux 下运行 ASP.NET 2.0
    sql left join inner jion 区别
    C# 图片识别(支持21种语言)
    5、Java并发编程:Lock
  • 原文地址:https://www.cnblogs.com/Tinamei/p/5162266.html
Copyright © 2011-2022 走看看