Selenium + ChromeDriver (1 ),
I have previously written some introductory knowledge about selenium + ChromeDriver. In this blog, I found qixinbao, a website that provides practical exercises. The structure of this blog is as follows: first, I will provide some links to get started with selenium + ChromeDriver. Second, I will explain some of my ideas and processes on crawling the website. Finally, I will provide the github address and sum up my experience. 1. for more information about environment configuration and getting started, see my previous blog: Workshop. for details about the crawling idea and the overall crawling idea of the process, there are two reasons: When you click the page to go to another page, the login or data verification code will appear. If you need to make a decision on every step, you should manage it directly in a method. Every time you click a page Link or button, you can go to the page judgment and processing process. This method is used to determine what the new page is and process different pages differently. Next, we will explain each step in detail. 2.1 click to log on.
1 // log on to the page and log on to 2 public static void toLoginAndLogin (WebDriver driver) {3 // 1. log on to the 4 List <WebElement> elements = driver.findElements(By.css Selector ("div. pull-right a "); 5 // 2. obtain the corresponding logon link through the subscript. 6. WebElement loginElement = elements. get (elements. size ()-1); 7 loginElement. click (); 8 // 2. If this method determines that the current page is a logon page, log on to CompanyInfoPageHandle. handleDifferentPage (driver); 10}View Code
2.2 search companies
// Query the company name public static void searchCompanyName (WebDriver driver, String CompanyName) {// 1. query the company name try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} // 2. Select the input box WebElement search = driver.findElement(By.css Selector ("input [placeholder = \" Enter keywords such as enterprise name, person name, and product name. Separate multiple keywords with spaces, for example, Shanghai Ping An \ "]"); search. sendKeys (CompanyName); // 3. Select the WebElement submit = driver.findElement(By.css Selector ("I. input-group-addon.search-btn.icon.icon-search "); submit. click (); // 4. determine the current page and process CompanyInfoPageHandle. handleDifferentPage (driver );}View Code
2.3 crawl the current data
/*** Save information ** @ param driver * @ param companyName */private void saveResult (WebDriver driver, String companyName) {try {Thread. sleep (2000); // 1. First, traverse the company information List of each row <WebElement> companyElementList = driver.findElements(By.css Selector ("div. col-xs-24.padding-v-1x.margin-0-0x.border-b-b4.company-item "); // 2, save the current line of company information for (WebElement companyElement: companyElementList) {// save information try {List <WebElement> companyInfoElementList = companyElement.findElements(By.css Selector ("div"); List <String> infoList = new ArrayList <> (); for (WebElement companyInfo: companyInfoElementList) {infoList. add (companyInfo. getText (). replace ("\ n", "");} String result = String. join ("\ t", infoList); FileUtils. saveLineAppend (companyName + ". txt ", result) ;}catch (Exception e) {continue ;}} catch (Exception e) {e. printStackTrace ();}}View Code 2.4 page judgment and processing in the previous process, as long as there is a click event triggered, You need to determine the page after the trigger is what page, in order to be able to process the current page. There are two main processes: the first step is to determine what the current page is, and the second step has different processing methods for different pages. For details, see the following code:
/*** Main method used to determine page conditions */public class CompanyInfoPageClassification {/*** page judgment, used to determine the current page * @ param driver * @ return */public static String PageClassificationMain (WebDriver driver) {if (isVerificationCodePage (driver) {return "Verification Code ";} if (is404Page (driver) {return "404" ;}if (isLoginPage (driver) {return "login" ;}return "normal ";} /*** determine whether the current page is a verification code page * @ param driver * @ return */private static bo Olean isVerificationCodePage (WebDriver driver) {List <WebElement> buttonList = driver.findElements(By.css Selector ("button. btn4"); if (buttonList. size ()! = 0 & buttonList. get (0 ). getText (). equals ("click to verify") {return true;} else {return false ;}} /*** determine whether the current page is 404 * @ param driver * @ return */private static boolean is404Page (WebDriver driver) {List <WebElement> buttonList = driver.findElements(By.css Selector ("div. error-container.error-404 "); if (buttonList. size ()! = 0) {return true;} else {return false ;}} /*** determine whether the current page is a logon page * @ param driver * @ return */private static boolean isLoginPage (WebDriver driver) {List <WebElement> buttonList = driver.findElements(By.css Selector ("input [placeholder = \" Enter your mobile phone number \ "]"); if (buttonList. size ()! = 0) {return true;} else {return false ;}}}View Code
* ** Perform different processing based on different pages */public class CompanyInfoPageHandle {public static void handleDifferentPage (WebDriver driver) {try {Thread. sleep (2000);} catch (Exception e) {e. printStackTrace ();} String pageStatus = CompanyInfoPageClassification. pageClassificationMain (driver); switch (pageStatus) {case "Verification Code": // 1. If it is a verification code page, you need to process it here, for example, manually enter the verification code. If the verification code is relatively simple, you can use the Character Recognition Method System. out. println ("Verification code page"); // 2. After logon, determine the page and process the current page handleDifferentPage (driver); break; case "login ": // 1. log on to the login (driver, "username", "password") on the logon page; // 2. Determine the page after logon, then process the current page handleDifferentPage (driver); break; case "404": break;} private static void login (WebDriver driver, String userName, String password) {WebElement phoneElement = driver.findElement(By.css Selector ("input [placeholder = \" Enter your mobile phone number \ "]"); phoneElement. sendKeys (userName); WebElement passwordElement = driver.findElement(By.css Selector ("input [placeholder = \" enter the password \ "]"); passwordElement. sendKeys (password); try {Thread. sleep (2000);} catch (InterruptedException e) {e. printStackTrace ();} List <WebElement> elements2 = driver.findElements(By.css Selector ("div. btn "); WebElement login = elements2.get (0); login. click ();}}View Code
3. github address and summary of experience github address: There is a tag under the li tag, as shown in the following code. If you only locate the li tag and call the click () method, an exception occurs. You still need to locate the tag and then click
<li><a href="/example" data-marko="{"onclick":"handleClickDebounce s0-9-20 false 0"}">2</a></li>2. Using selenium + ChromeDriver for crawling cannot be fully automated. For this website, you need to log on when the click frequency is too high. Therefore, I log on to the account directly from the beginning. After logging on to the system and clicking the frequency, you need to enter the verification code. In this case, if the verification code is a common number, you can also use the character recognition technology. However, the verification code of this website is hard to be recognized by machines. 3. Using selenium + ChromeDriver for crawling is actually a very bad thing. It is actually very easy for others to reverse your attacks. Step 1: Determine the request frequency of each ip address in the tourist mode. If the request frequency exceeds a certain frequency, You need to log on.
Step 2: After step 1 is completed, you will have an account, which is directly determined by the request frequency or frequency of the account. If the request frequency exceeds a certain frequency or frequency, you need to perform human-machine verification. The current verification code is very disgusting, and it is no longer the same as the previous character 4. of course, using selenium + ChromeDriver can still crawl some data, but manual intervention is required in the specific process. When encountering the verification code, you must pause the thread and enter the verification code. However, the main purpose of selenium itself is not crawler, But Automated Testing of web pages. If you want to crawl data, you still need to use a proxy to directly send an Interface request to obtain persistent and stable data.