1,环境配置(windows)
(1)下载Pythonhttps://www.python.org/doc/
(2)下载pip源码包 : https://pypi.python.org/pypi/pip
(3)pip install selenium
(4)安装ChromeDriver, 该工具供selenium使用Chrome.
ChromeDriver: http://npm.taobao.org/mirrors/chromedriver/
将ChromeDriver,解压,放到python的文件夹bin目录下
2,开始写python代码.
(1),导入需要的包
from selenium import webdriver
import random`
(2)加载驱动
driver = webdriver.Chrome()
(3)打开目标网址
driver.get('https://www.wjx.cn/m/55386119.aspx')
(4)在浏览器中按f12 打开开发者模式,分析html标签
(5)根据具体的标签名,class名,id名称来获取,填写列表(此处具体问题具体分析)
例如
answers = driver.find_elements_by_css_selector('.ui-controlgroup')
for answer in answers:
try:
driver.execute_script("arguments[0].scrollIntoView();", answer)
ans = answer.find_elements_by_css_selector('.ui-radio')
li = random.choice(ans)
li.click()
except Exception as e:
print(e)
beginner_problem = driver.find_element_by_css_selector('.beginner_problem')
text_area =beginner_problem.find_element_by_css_selector('textarea')
text_area.send_keys('无')
q5 = driver.find_elements_by_css_selector('.textCont')
driver.execute_script("arguments[0].scrollIntoView();", q5[0])
print(q5)
q5[0].send_keys('广东')
(6)提交并关闭页面
submit_button = driver.find_element_by_css_selector('#ctlNext')
submit_button.click()
driver.close()