Python3.5 Webdiver 刷点击量及常见问题
望共同进步
转载请注明地址:https://blog.csdn.net/weixin_39701039/article/details/79668890
之前接触到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等定位有所疑问可以提出,我尽量抽时间整理一下。
望有所帮助,望采纳!!