其实,站在数据底层理解,我们对网页的操作可以抽象成向服务器发送数据。
这样看来,我们有两种方法使用Python模拟网页按钮点击:
方法一:间接模拟。模拟向服务器发送数据。可以用抓包工具,看点击按钮时到底向服务器发送的是什么数据,然后使用python模拟发送的数据。
方法二:真实模拟。可以先找到按钮,然后执行点击。这个有现成的库,Selenium。
用python的sendkeys直接模拟键盘,用ctype扩展来点鼠标。你需要做的就是用python打开浏览器,然后输入网站,在找到按钮的坐标(固定到程序里),然后点击就行了。不过简单的可以,复杂点的就要考虑很多问题了。先在命令行中下载一个selenium库:
python -m pip install selenium然后使用selenium中的webdriver来进行模拟网页点击:
from selenium import webdriverfrom selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
sel=webdriver.Chrome() #也可换成Ie(),Firefox()等
element=sel.find_element() #在网页源码中查找元素,具体可参考http://www.aichengxu.com/python/11949.htm
element.click() #模拟对元素进行点击
如果你是Chrome用户,需要手动下载一个chromedriver.exe,这里附上,把它放入系统Path路径任意一个文件夹中即可