如何利用Python進行基礎自動點擊爬蟲
公司里面有很多工作都是重復性的,繁瑣的,因此,為了節省大家的工作時間,現可利用python技術,使大家能夠實現“自動化”點擊操作,大大減少了需要自動點擊的工作量,下述,我們會以“客服系統自動點擊程序”為例,給大家講一下如何創建“自動化編程”
第一步:導入相關的工具類
#導入相關的工具類
from bs4 import BeautifulSoup
from selenium import webdriver
import selenium
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
import json
import requests
import demjson
import datetime
import time
import re
import copy
from lxml import etree
import os
import time
import pandas as pd
import openpyxl
import pymysql
import glob
from sqlalchemy import create_engine
# from docx import Document
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
第二步:利用pandas類打開需要操作的EXCEL文檔(如果需要)
#利用pd類的read_excel方法,把括號內填入路徑的文件導入到程序中,新增變量df進行“接收”
df=pd.read_excel(r'增值服務取消審核單(1).xlsx')
#輸出excel表數據
df
第三步:打開“自動化瀏覽器”
seleniumGoo=webdriver.Chrome()
seleniumGoo.maximize_window()
seleniumGoo.get("http://10.192.48.82/index")
第四步:網頁中輸入相關信息
在自動化網頁后,輸入自己的賬號信息,並到達相應的網頁
第五步:編寫核心爬蟲代碼,並進行調試
seleniumGoo.switch_to_window(handles[0])
search_result=seleniumGoo.find_element_by_xpath('//*[@id="workidNumber"]')
search_result.click()
search_result.clear()
search_result.send_keys(df['案件號'][i])
search_result=seleniumGoo.find_element_by_xpath('//*[@id="search"]')
search_result.click()
#點擊繼續訪問網頁
seleniumGoo.find_element_by_xpath('//*[@id="bootStrap"]/tbody/tr/td[12]/a[2]')click()
#browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="bootStrap"]/tbody/tr/td[12]/a[2]'))).click()
handles = seleniumGoo.window_handles
print(handles[1])
seleniumGoo.switch_to_window(handles[1])
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="quanju"]/div[2]/div[6]/label[2]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="quXiao"]/option[2]'))).click()
time.sleep(0.5)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,' //*[@id="subReset1"]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="subReset"]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[2]/div[2]/button'))).click()
time.sleep(1)
handles = seleniumGoo.window_handles
seleniumGoo.switch_to_window(handles[0])
第六步:將核心代碼進行函數封裝
以客服系統為例:
def hexin():
seleniumGoo.switch_to_window(handles[0])
search_result=seleniumGoo.find_element_by_xpath('//*[@id="workidNumber"]')
search_result.click()
search_result.clear()
search_result.send_keys(df['案件號'][i])
search_result=seleniumGoo.find_element_by_xpath('//*[@id="search"]')
search_result.click()
#點擊繼續訪問網頁
seleniumGoo.find_element_by_xpath('//*[@id="bootStrap"]/tbody/tr/td[12]/a[2]')click()
#browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="bootStrap"]/tbody/tr/td[12]/a[2]'))).click()
handles = seleniumGoo.window_handles
print(handles[1])
seleniumGoo.switch_to_window(handles[1])
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="quanju"]/div[2]/div[6]/label[2]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="quXiao"]/option[2]'))).click()
time.sleep(0.5)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,' //*[@id="subReset1"]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="subReset"]'))).click()
time.sleep(1)
browser_sel = WebDriverWait(seleniumGoo, 40).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[3]/div[2]/div[2]/button'))).click()
time.sleep(1)
handles = seleniumGoo.window_handles
seleniumGoo.switch_to_window(handles[0])
第七步:編寫自動化循環方法,並將封裝好的“核心代碼”放入其中
def autoLoop(df):
#讀取df表的行數,來判定for循環的次數
for i in range(len(df)):
try:
#爬蟲核心代碼
hexin()
except:
#如遇到錯誤,則繼續下一條記錄
continue
第八步:運行autoLoop自動化循環方法
#運行autoLoop方法
autoLoop()
這就是Python進行基礎自動點擊爬蟲啦