先在命令行中下载一个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路径任意一个文件夹中即可
首页,我们先理解网页点击的本质:其实,站在数据底层理解,我们对网页的操作可以抽象成向服务器发送数据。
这样看来,我们有两种方法使用Python模拟网页按钮点击:
方法一:间接模拟。模拟向服务器发送数据。可以用抓包工具,看点击按钮时到底向服务器发送的是什么数据,然后使用python模拟发送的数据。
方法二:真实模拟。可以先找到按钮,然后执行点击。这个有现成的库,Selenium。
一种方法是通过urllib2,beautifulsoup等库解析网页获取next元素里面的url,直接跳转url,另外一种方法是selenium直接锁定next元素执行.click()操作,一种比较省时,一种比较省力,自行选择