Python3.5 Webdiver 刷點擊量及常見問題

Python3.5 Webdiver 刷點擊量及常見問題

望共同進步

轉載請註明地址:https://blog.csdn.net/weixin_39701039/article/details/79668890

來一個小插曲 爲了註冊 http://nianjian.xiaze.com/tags.php?/%E4%B8%AD%E5%9B%BD%E7%B2%BE%E7%A5%9E%E6%96%87%E6%98%8E%E5%B9%B4%E9%89%B4/1/13522221401/

之前接觸到webdriver是在爲了模擬登陸爬蟲的時候,後來因爲工作原因就沒有再研究它了,近幾天想到了用它來刷點擊量試試(畢竟自己的博客訪問量太少委屈,開玩笑哈,其實就是實驗的時候用了下,並沒有用webdriver來刷點擊  大笑

在實驗的時候我將總結一下我遇到的一些問題:

剛開始我很天真的用爬蟲訪問網址委屈,發現點擊量並沒有增加

然後着手用Webdriver模擬點擊(click):

一:觸不可及

       Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.16299 x86_64)

代碼如下:

#coding:utf-8
#python3.5.1
from selenium import webdriver
import os
import time
#chromedriver.exe路徑
path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
#配置環境
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

driver.get('https://blog.csdn.net/weixin_39701039')

url = "'https://blog.csdn.net/weixin_39701039/article/details/79563012'"

time.sleep(5)
#xpath定位在Python3.5 定義函數
driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href='https://blog.csdn.net/weixin_39701039/article/details/79563012']").click()
time.sleep(5)
driver.quit()

#結果:


當時就看不懂

因爲我如果定位在

代碼如下:

#coding:utf-8
#python3.5.1
from selenium import webdriver
import os
import time
#chromedriver.exe路徑
path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
#配置環境
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

driver.get('https://blog.csdn.net/weixin_39701039')

url = "'https://blog.csdn.net/weixin_39701039/article/details/79655795'"

time.sleep(5)
#xpath定位在Python3.5 類和實例
driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href='https://blog.csdn.net/weixin_39701039/article/details/79655795']").click()
time.sleep(5)
#driver.quit()

#結果:



沒有問題啊,不就是一個點擊在上面,一個在下面麼,有什麼區別呢,後來我就看模擬打開的網頁:


發現唯一的區別在於,想要點擊 Python3.5 定義函數 在最底部呢,模擬點擊,顧名思義,你人得看得到,點擊的到才行啊,後來我試了一下上面和下面的幾個xpath定位,發現問題的根源: 當前窗口觸不可及,所以出錯

那麼解決的問題就需要用鼠標把它拉下去,或者滾動鼠標啦


代碼如下:


#coding:utf-8
#python3.5.1
from selenium import webdriver
import os
import time
#chromedriver.exe路徑
path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
#配置環境
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

driver.get('https://blog.csdn.net/weixin_39701039')

url = "'https://blog.csdn.net/weixin_39701039/article/details/79563012'"

time.sleep(5)
#xpath定位在Python3.5 定義函數
driver.execute_script('scroll(0,200)')
driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href='https://blog.csdn.net/weixin_39701039/article/details/79563012']").click()
time.sleep(5)
#driver.quit()

#結果:



這樣這個問題就解決了,然而並沒有那麼順利

二:ConnectionRefusedError: [WinError 10061] 由於目標計算機積極拒絕,無法連接。

刷點擊麼,那首先考慮的是刷,循環起來多舒服,然後我就寫了個循環:

代碼如下:

#coding:utf-8
#python3.5.1
from selenium import webdriver
import os
import time
#chromedriver.exe路徑
path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
#配置環境
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

for i in range(2):

    driver.get('https://blog.csdn.net/weixin_39701039')

    url = "'https://blog.csdn.net/weixin_39701039/article/details/79563012'"

    time.sleep(5)
    #xpath定位在Python3.5 定義函數
    driver.execute_script('scroll(0,200)')
    driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href='https://blog.csdn.net/weixin_39701039/article/details/79563012']").click()
    time.sleep(5)
    driver.quit()

#結果:

ConnectionRefusedError: [WinError 10061] 由於目標計算機積極拒絕,無法連接。

打開一次正常,在循環到第二次的時候出錯了,爲啥,模擬點擊太快?,然後我在循環里加了個time.sleep(60);然而結果還是一樣,我百度搜索了一下,發現可能是因爲雖然driver.quit(),但python進程未關閉吧,然而還是沒有找到解決的辦法,那就換個思路唄:

我不關閉(先不使用driver.quit()),循環過一段時間點擊,

但是這樣打開的窗口有點多啊,所以可以循環關閉點擊的窗口

代碼如下:

#coding:utf-8
#python3.5.1
from selenium import webdriver
import os
import time
#chromedriver.exe路徑
path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
#配置環境
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)

for i in range(3):
    driver.get('https://blog.csdn.net/weixin_39701039')
    url = "'https://blog.csdn.net/weixin_39701039/article/details/79563012'"
    time.sleep(5)
    #xpath定位在Python3.5 定義函數
    driver.execute_script('scroll(0,200)')
    driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href='https://blog.csdn.net/weixin_39701039/article/details/79563012']").click()
    time.sleep(5)
    #獲得第一個打開窗口的句柄
    search_windows = driver.current_window_handle
    #獲取所以窗口句柄
    all_handles = driver.window_handles
    #循環句柄
    for handle in all_handles:
        if handle == search_windows:
            pass
        else:
            driver.switch_to_window(handle)
            #關閉當前窗口
            driver.close()
    #返回第一個窗口
    driver.switch_to_window(search_windows)
    time.sleep(20)
    print('點擊第' + str(i + 1) + '次已完成')
#關閉所有窗口
driver.quit()

#結果:


這樣這個問題就解決了哈,但是注意time.sleep()的設置,點擊過快,點擊量並不會增加,所以可以適當的延遲一下

解決完問題之後

以下代碼是循環點擊多個的例子,可以看一下:

#coding:utf-8
#python3.5.1

from selenium import webdriver

import os
import time
from selenium.webdriver.common.action_chains import ActionChains

path_d = r'C:\Program Files (x86)\Google\Chrome\Application' + '/' + 'chromedriver.exe'
chromedriver = path_d
os.environ["webdriver.chrome.dirver"] = chromedriver
driver = webdriver.Chrome(chromedriver)


urls = ["'https://blog.csdn.net/weixin_39701039/article/details/79655795'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79642604'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79576549'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79567006'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79563012'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79558279'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79550067'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79544265'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79535578'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79527224'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79522558'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79513650'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79504931'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79492672'",
        "'https://blog.csdn.net/weixin_39701039/article/details/79423191'"


        ]

driver.get('https://blog.csdn.net/weixin_39701039')
#循環點擊次數
for x in range(2):
    i = 0
    #循環點擊多個
    for url in urls:
        i = i + 1
        down = 100*i
        #每次鼠標向下多滾動100,定位
        coordinate = 'scroll(0,' + str(down) + ')'
        driver.execute_script(coordinate)
        try:
            driver.find_element_by_xpath("//li[@class='blog-unit']//a[@href=" + url + "]").click()
            time.sleep(5)

        except:
            continue
    search_windows = driver.current_window_handle
    all_handles = driver.window_handles

    for handle in all_handles:
        if handle == search_windows:
            pass
        else:

            driver.switch_to_window(handle)
            driver.close()

    driver.switch_to_window(search_windows)
    print('點擊第' + str(i + 1) + '次已完成')
    time.sleep(20)

driver.quit()

當然刷點擊還是不要啦,這個當做實驗可以的,還是需要大家自己的點擊來認同比較開心大笑!!!謝謝!!!

大家關於xpath等定位有所疑問可以提出,我儘量抽時間整理一下。

望有所幫助,望採納!!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章