发布:2020/9/24 14:19:38作者:管理员 来源:本站 浏览次数:1039
先上结果:
之前 在公司业务中用过java+Selenium+ChromeDriver ,使用起来非常顺手,可以完美模拟真实的用户浏览行为。最近休息的时候想用C#也试一下,于是有了本文。
实现原理一样,只是由java换成了C#。(ps:个人感觉就业务开发代码来说,熟悉之后两种语言可以无缝切换。)
事先声明,代码中会采集用户登录咕咚网站之后的个人数据接口,如果此行为损害了咕咚网站的利益,请联系我删除或修改本文(我对采集行为一直本着每一次调用之后sleep的原则,毕竟不是为了把人家的网站搞死)。文中使用的数据是我自己的跑步数据,不涉及到其他用户的数据。
项目结构:为了方便跑友使用,用的winform程序,附nuget包
以下是form1.cs的代码,删除了部分提示的 MessageBox.Show(),从产品角度来讲,应该添加loading提示和进度条,这里就只放代码了。需要安装最新的chrome浏览器+代码中使用的chromedriver是 v2.9.248315
using Newtonsoft.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GuDongLine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label2.Text = "请根据提示进行操作,本产品需要最新版本Chrome浏览器支持,网络良好的情况下运行,内部交流,不作为商业软件,如有侵权请扫中间微信二维码联系我。祝各位跑友生活愉快,身体健康,happy PB";
}
delegate void ChangeInvoke(int num);
public int Fun1;
private void button1_Click(object sender, EventArgs e)
{
try
{
Thread thread = new Thread(go);
thread.Start();
}
catch (Exception)
{
MessageBox.Show("操作异常,请重新打开,或联系我");
}
}
private void ChangeNum()
{
MessageBox.Show("正在运行请等待!运行完毕后会有提示!");
}
public void go()
{
if (MessageBox.Show("下面请登录咕咚,本软件只会向咕咚官网发送和接受数据,如果发现软件向其他网址发送数据,说明本软件被修改,可能有病毒", "登录咕咚账号", MessageBoxButtons.OKCancel).Equals(DialogResult.OK))
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--test-type", "--ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(System.Environment.CurrentDirectory, options);
driver.Url = "http://www.codoon.com/home";
Thread.Sleep(10000);
driver.Navigate().GoToUrl("http://www.codoon.com/gps_sports/my_routes");
Thread.Sleep(5000);
int runCount = int.Parse(driver.FindElement(By.Id("current_index")).Text) / 3;
for (int i = 0; i < runCount + 5000; i++)
{
try
{
driver.FindElement(By.ClassName("more_data")).Click();
Thread.Sleep(800);
}
catch (Exception)
{
if (i > runCount + 100)
{
break;
}
}
}
Thread thread2 = new Thread(ChangeNum);
thread2.Start();
string userId = driver.FindElement(By.ClassName("home_user_header")).GetAttribute("src").Substring(33, 36);
string data = "";
dynamic va = driver.FindElements(By.ClassName("detail_sports_content"));
foreach (IWebElement iwe in va)
{
using (var client = new WebClient())
{
Thread.Sleep(200);
var responseString = client.DownloadString("http://www.codoon.com/gps_sports/route?user_id=" + userId + "&route_id=" + iwe.FindElement(By.TagName("table")).GetAttribute("id") + "&need_next=1&_=1520349266435");
var DynamicObject = JsonConvert.DeserializeObject<dynamic>(responseString);
string ss = "";
foreach (var s in DynamicObject.line)
{
ss += ((double)s[1] + 0.0062).ToString() + "," + ((double)s[0] + 0.00135).ToString() + ";";
}
ss = ss.Remove(ss.Length - 1);
data += ss + "&";
}
}
data = data.Remove(data.Length - 1);
driver.Close();
string html = @"<!DOCTYPE html><html lang='zh - CN\'>
<head>
<meta charset = 'utf-8'>
<meta http - equiv = 'X-UA-Compatible' content = 'IE=edge'>
<meta name = 'viewport' content = 'width=device-width, initial-scale=1'>
<title> my_all_run_lines </title>
<link rel = 'stylesheet' href = 'http://cache.amap.com/lbs/static/main1119.css' />
</head>
<body>
<div id = 'container' class='container'></div>
<script src = 'http://webapi.amap.com/loca?key=6ac3f558819c2c4711ea0c0a37192137'></script>
<script src='http://a.amap.com/Loca/static/dist/jquery.min.js'></script>
<script>
var map = Loca.create('container', {
key: '1e387d3db027b46a23600cf7f2ed7344',
mapStyle: 'amap://styles/grey',
features: ['bg', 'road'],
zoom: 10
});
var layer = Loca.visualLayer({
container: map,
type: 'line',
shape: 'line',
});
$.get('http://a.amap.com/Loca/static/mock/buslines.txt', function (data) {
data = '" + data + "';" + "" +
"" +
"" +
"" +
"" +
"" + @"var lines = data.split('&').map(function (item) {
return {
linePath: item.split(';').map(function(lnglat) {
return lnglat.split(',');
})
};
});
layer.setData(lines, {
lnglat: 'linePath'
});
layer.setOptions({
style: {
opacity: 0.2,
lineWidth: 2,
stroke: '#b7eff7',
}
});
layer.render();
})
</script>
</body>
</html>";
string path = System.Environment.CurrentDirectory + "\\map.html";
File.WriteAllText(path, html);
}
else
{
System.Environment.Exit(0);
}
}
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4