c 抓取网页数据(一下提取数据中的部分源码,你知道吗?(图))

优采云 发布时间: 2021-11-13 13:21

  c 抓取网页数据(一下提取数据中的部分源码,你知道吗?(图))

  2:获取网页源码

  3:检查源码中是否有我们需要提取的数据

  4:反汇编源码,一般使用分段、常规或第三方jar包

  5:获取需要的数据并给自己创建的对象赋值

  6:数据提取和保存

  下面简单说一下提取数据中的部分源代码及其用途:

   /**

* 向指定URL发送GET方法的请求

*

* @param url

* 发送请求的URL

* @param param

* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。

* @return URL 所代表远程资源的响应结果

*/

public static String sendGet(String url, String param) {

String result = "";

BufferedReader in = null;

try {

String urlNameString = url;

URL realUrl = new URL(urlNameString);

// 打开和URL之间的连接

URLConnection connection = realUrl.openConnection();

// 设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent",

"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

// 建立实际的连接

connection.connect();

// 获取所有响应头字段

Map map = connection.getHeaderFields();

// 定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(new InputStreamReader(

connection.getInputStream())); //这里如果出现乱码,请使用带编码的InputStreamReader构造方法,将需要的编码设置进去

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

System.out.println("发送GET请求出现异常!" + e);

e.printStackTrace();

}

// 使用finally块来关闭输入流

finally {

try {

if (in != null) {

in.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

return result;

}

  

  解析存储的数据

  public Bid getData(String html) throws Exception {

//获取的数据,存放在到Bid的对象中,自己可以重新建立一个对象存储

Bid bid = new Bid();

//采用Jsoup解析

Document doc = Jsoup.parse(html);

// System.out.println("doc内容" + doc.text());

//获取html标签中的内容tr

Elements elements = doc.select("tr");

System.out.println(elements.size() + "****条");

//循环遍历数据

for (Element element : elements) {

if (element.select("td").first() == null){

continue;

}

Elements tdes = element.select("td");

for(int i = 0; i < tdes.size(); i++){

this.relation(tdes,tdes.get(i).text(),bid,i+1);

}

}

return bid;

}

  

  获得的数据

  Bid {

h2 = \'详见内容\',

itemName = \'诉讼服务中心设备采购\',

item = \'货物/办公消耗用品及类似物品/其他办公消耗用品及类似物品\',

itemUnit = \'详见内容\',

areaName = \'港北区\',

noticeTime = \'2018年10月22日 18:41\',

itemNoticeTime = \'null\',

itemTime = \'null\',

kaibiaoTime = \'2018年10月26日 09:00\',

winTime = \'null\',

kaibiaoDiDian = \'null\',

yusuanMoney = \'¥67.00元(*敏*感*词*)\',

allMoney = \'null\',

money = \'null\',

text = \'\'

}

  

  

  

  ​

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线