项目要做一个四个层级栏的数据抓取,而且点击查询后数据会在新跳出的网页。
源码如下 注释解释
from selenium import webdriver import selenium #from time import sleep as sp url=‘http://202.127.42.157/moazzys/nongqing.aspx‘ site=webdriver.Chrome() site.get(url) #data={} mainhandle=site.current_window_handle#主页面句柄 每个浏览器标签页都有一个句柄 years=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_year"]/option‘) co_year=len(years) for d in range(co_year): years[d].click() provs=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_prov"]/option‘) co_prov=len(provs) for c in range(co_prov): provs[c].click() items=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_item"]/option‘) co_item=len(items) for b in range(co_item): items[b].click() types=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_type"]/option‘) co_type=len(types) for a in range(co_type): types[a].click() #点击进入新窗口 site.find_element_by_id(‘ContentPlaceHolder1_btnsearch‘).click() handles = site.window_handles for handle in handles:# 轮流得出标签页的句柄 切换窗口 因为只有两个标签页实际是假for循环 if handle!=mainhandle: site.switch_to_window(handle) #获得数据 try: raw=site.find_element_by_xpath(‘//tr/td[@align="right"]‘) print (raw.text) #data.append(raw.text) except Exception as e: print("无该数据") #获得数据 site.close() #关闭当前标签页 site.switch_to_window(mainhandle)#回到原来标签页 types=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_type"]/option‘) types[a].click() items=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_item"]/option‘) items[b].click() provs=types=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_prov"]/option‘) provs[c].click() year=types=site.find_elements_by_xpath(‘//select[@id="ContentPlaceHolder1_year"]/option‘) years[d].click() site.quit()
原文地址:https://www.cnblogs.com/batt1ebear/p/9813584.html
时间: 2024-11-05 16:00:43