Using C+Selenium+Chrome Driver to Generate My Running Route Map

Keywords: C# Amap Selenium Java Google

First result:

 

 

 

Previously, I used java + Selenium + Chrome Driver in my company's business, which is very handy and can simulate the real user's browsing behavior perfectly. I want to try C # during my recent break, so I have this article.

The implementation principle is the same, but it's just changed from java to C#. (ps: Personally, I feel that when I am familiar with business development code, the two languages can switch seamlessly.)

In advance, the code will collect the user's personal data interface after logging on to Grunda website. If this behavior harms the interests of Grunda website, please contact me to delete or modify this article (I always follow the principle of sleep after every call, after all, it is not to kill someone else's website). The data used in this paper is my own running data, not involving other users'data.

Project structure: In order to facilitate the use of runners, the winform program with nuget package is used.

 

 

Following is the code for form1.cs, which deletes part of the prompt MessageBox.Show(). From the product point of view, loading prompt and progress bar should be added, and only the code will be put here. The chrome driver used in the latest Chrome browser + code needs to be installed is v2.9.248315

  1 using Newtonsoft.Json;
  2 using OpenQA.Selenium;
  3 using OpenQA.Selenium.Chrome;
  4 using System;
  5 using System.Collections.Generic;
  6 using System.ComponentModel;
  7 using System.Data;
  8 using System.Drawing;
  9 using System.IO;
 10 using System.Linq;
 11 using System.Net;
 12 using System.Text;
 13 using System.Threading;
 14 using System.Threading.Tasks;
 15 using System.Windows.Forms;
 16 
 17 namespace GuDongLine
 18 {
 19 public partial class Form1 : Form
 20 {
 21 public Form1()
 22 {
 23 
 24 InitializeComponent();
 25 label2.Text = "Please follow the instructions. This product needs the latest version. Chrome Browser support, good network operation, internal communication, not as a commercial software, if there is infringement, please contact me by browsing the two-dimensional code of Wechat. I wish all runners a happy life and good health. happy PB";
 26 }
 27 delegate void ChangeInvoke(int num);
 28 public int Fun1;
 29 private void button1_Click(object sender, EventArgs e)
 30 {
 31 try
 32 {
 33 Thread thread = new Thread(go);
 34 thread.Start();
 35 
 36 }
 37 catch (Exception)
 38 {
 39 MessageBox.Show("Abnormal operation, please reopen, or contact me");
 40 }
 41 
 42 }
 43 private void ChangeNum()
 44 {
 45 MessageBox.Show("Please wait while it is running! There will be prompts after running!");
 46 }
 47 public void go()
 48 {
 49 if (MessageBox.Show("Now please log on to Gudong. This software only sends and receives data to Gudong's official website. If you find that the software sends data to other websites, it indicates that the software has been modified and may have viruses.", "Log on to Gudong account", MessageBoxButtons.OKCancel).Equals(DialogResult.OK))
 50 {
 51 ChromeOptions options = new ChromeOptions();
 52 options.AddArguments("--test-type", "--ignore-certificate-errors");
 53 IWebDriver driver = new ChromeDriver(System.Environment.CurrentDirectory, options);
 54 driver.Url = "http://www.codoon.com/home";
 55 Thread.Sleep(10000);
 56 
 57 driver.Navigate().GoToUrl("http://www.codoon.com/gps_sports/my_routes");
 58 
 59 
 60 Thread.Sleep(5000);
 61 int runCount = int.Parse(driver.FindElement(By.Id("current_index")).Text) / 3;
 62 for (int i = 0; i < runCount + 5000; i++)
 63 {
 64 try
 65 {
 66 driver.FindElement(By.ClassName("more_data")).Click();
 67 Thread.Sleep(800);
 68 }
 69 catch (Exception)
 70 {
 71 if (i > runCount + 100)
 72 {
 73 break;
 74 }
 75 
 76 }
 77 }
 78 Thread thread2 = new Thread(ChangeNum);
 79 thread2.Start();
 80 string userId = driver.FindElement(By.ClassName("home_user_header")).GetAttribute("src").Substring(33, 36);
 81 string data = "";
 82 dynamic va = driver.FindElements(By.ClassName("detail_sports_content"));
 83 foreach (IWebElement iwe in va)
 84 {
 85 using (var client = new WebClient())
 86 {
 87 Thread.Sleep(200);
 88 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");
 89 
 90 
 91 var DynamicObject = JsonConvert.DeserializeObject<dynamic>(responseString);
 92 string ss = "";
 93 foreach (var s in DynamicObject.line)
 94 {
 95 ss += ((double)s[1] + 0.0062).ToString() + "," + ((double)s[0] + 0.00135).ToString() + ";";
 96 }
 97 ss = ss.Remove(ss.Length - 1);
 98 data += ss + "&";
 99 }
100 }
101 data = data.Remove(data.Length - 1);
102 driver.Close();
103 string html = @"<!DOCTYPE html><html lang='zh - CN\'>
104 <head>
105 
106 <meta charset = 'utf-8'>
107 
108 <meta http - equiv = 'X-UA-Compatible' content = 'IE=edge'>
109 
110 <meta name = 'viewport' content = 'width=device-width, initial-scale=1'>
111 
112 <title> my_all_run_lines </title>
113 
114 <link rel = 'stylesheet' href = 'http://cache.amap.com/lbs/static/main1119.css' />
115 </head>
116 <body>
117 <div id = 'container' class='container'></div>
118 <script src = 'http://webapi.amap.com/loca?key=6ac3f558819c2c4711ea0c0a37192137'></script>
119 <script src='http://a.amap.com/Loca/static/dist/jquery.min.js'></script>
120 <script>
121 
122 var map = Loca.create('container', {
123 key: '1e387d3db027b46a23600cf7f2ed7344',
124 mapStyle: 'amap://styles/grey',
125 features: ['bg', 'road'],
126 zoom: 10
127 });
128 
129 var layer = Loca.visualLayer({
130 container: map,
131 type: 'line',
132 shape: 'line',
133 });
134 
135 $.get('http://a.amap.com/Loca/static/mock/buslines.txt', function (data) {
136 data = '" + data + "';" + "" +
137 "" +
138 "" +
139 "" +
140 "" +
141 "" + @"var lines = data.split('&').map(function (item) {
142 return {
143 linePath: item.split(';').map(function(lnglat) {
144 return lnglat.split(',');
145 })
146 };
147 });
148 
149 layer.setData(lines, {
150 lnglat: 'linePath'
151 });
152 layer.setOptions({
153 style: {
154 opacity: 0.2,
155 lineWidth: 2,
156 stroke: '#b7eff7',
157 }
158 });
159 
160 layer.render();
161 })
162 
163 </script>
164 </body>
165 </html>";
166 
167 string path = System.Environment.CurrentDirectory + "\\map.html";
168 File.WriteAllText(path, html);
169 }
170 else
171 {
172 System.Environment.Exit(0);
173 }
174 }
175 
176 }
177 }
178 
179  

 

Talk about ideas:

1. Determine the language used and the acquisition framework.

2. Determine the source of data, from Grunda app or Grunda website, if Grunda website does not provide an interface, it is estimated that we will have to study app. One of the time-consuming points in this article is to study the rules of requesting data from the Grunda website. In principle, as long as the normal users can open the web page, they can not block the collection tool.

3. Data display: find the api interface of Baidu Map and Golden Map, and try them separately. In fact, Golden Web itself is using Google, but the interface of Google is not convenient to study. Finally, the Golden Map is selected from the effect of data display. By the way, the running data collected from Golden is actually a group of longitude and latitude seats. Standard, and is castrated longitude and latitude, accuracy is not enough, so there will be deviation when the map is displayed, with the increase of running distance, the amount of data is relatively large. In this paper, 1700 kilometers of running, the generated display html is 2.42M, my family's antique notebook loading time is relatively long.

 

 

Another: I called Gudao's customer service a long time ago and wanted a function that integrated all my running routes. It seemed promising, but I didn't find this function in app (the origin of this article?). Of course, it's not very good for the server to load so much data at a time if you provide this function, but can you make a detour, such as only one month ago? Generated after application? Personally, I feel that there are many solutions, but also can be a marketing campaign.

 

Welcome to discuss, welcome to advise, thank you, I hope every programmer is healthy.

by: Running over the coder of the 16+17 North Horse

Posted by scanreg on Sun, 19 May 2019 04:59:44 -0700