selenium是一个可以自动化驱动系统的网页浏览器的工具,可以完美模拟用户操作,从用户的角度来操作浏览器。
1.首先python中安装selenium。
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
2.本例中选择用selenium驱动系统自带的edge浏览器,必须查看系统当前edge版本,在网站https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
寻找对应的准确版本,不能下载使用其他版本。
3.下载后解压文件,找到exe文件,更名MicrosoftWebDriver.exe。放入当前工程文件夹。
4.通过语句webdriver.Edge()调用。
5.弹出系统被调用的浏览器,证明自动调用系统浏览器成功,之后可以进行进一步自动化操作开发。

附带一个查询城市天气的代码:(openweathermap经测试现已暂时无法查询)
import re from selenium import webdriver driver = webdriver.Edge() city = input('输入要查询的城市:').lower() driver.get(r'http://openweathermap.org/find?q={0}'.format(city)) content = driver.page_source.lower() matchResult = re.search(r'<a herf="(.+?)">\s+' + city + '.+?', content) if matchResult: print(matchResult.group(0)) else: print('查不到,请检查城市名字')
浙公网安备 33010602011771号