United States Department of Agriculture 美国农业部网站数据自动下载小脚本
帮对象写的自动点击页面的小脚本,如果有需要的可以试试,(#^.^#)
https://apps.fas.usda.gov/gats/ExpressQuery1.aspx
1 var year = '1991'; 2 3 var box3 = document.querySelector("#ctl00_ContentPlaceHolder1_lb_Partners"); 4 box3.value='ALLRG'; 5 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_lb_Products"); 6 box3.value='M1'; 7 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlValueUnit"); 8 box3.value='M'; 9 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlDateSeries"); 10 box3.value='Annual'; 11 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlOrderBy"); 12 box3.value='CODE'; 13 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlInDetail"); 14 box3.value='Product'; 15 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlCalculation"); 16 box3.value=''; 17 18 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlStartYear"); 19 box3.value= year; 20 box3.dispatchEvent(new Event('change')); 21 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_ddlEndYear"); 22 box3.value= year; 23 box3.dispatchEvent(new Event('change')); 24 25 26 function clickStep() 27 { 28 if (document.querySelector("#overlay").style.display == 'block') { 29 setTimeout("clickStep()", 1000); 30 return; 31 } 32 console.log("触发点击事件") 33 document.querySelector("#ctl00_ContentPlaceHolder1_btnRetrieveData").click(); 34 } 35 setTimeout("clickStep()", 1000); 36 37 String.prototype.startWith=function(str){ 38 var reg=new RegExp("^"+str); 39 return reg.test(this); 40 } 41 var clicked = false; 42 function doExpand() { 43 box3 = document.querySelector("#ctl00_ContentPlaceHolder1_UltraWebTab1__ctl1_grdExpressQuery_GridView1"); 44 var rows = box3.rows; 45 var inputs = []; 46 for (var index in rows) { 47 var length = rows[index].cells ? rows[index].cells.length : 0; 48 if (length < 4) { 49 continue; 50 } 51 var nodes = rows[index].cells[3].childNodes; 52 if (nodes.length == 0) { 53 continue; 54 } 55 if (nodes[0].nodeName == '#text') { 56 continue; 57 } 58 var cell5Text = rows[index].cells[4].innerText; 59 if (!cell5Text.startWith('Bulk Total') && !cell5Text.startWith('Intermediate Total') && !cell5Text.startWith('Consumer Oriented Total')) { 60 continue; 61 } 62 63 var input = nodes[0]; 64 65 if(input.value == '-') { 66 continue; 67 } 68 69 clicked = true; 70 input.click(); 71 break; 72 } 73 } 74 75 var index = 0; 76 function expandStep() 77 { 78 if (document.querySelector("#overlay").style.display == 'block' || document.querySelector("#ctl00_ContentPlaceHolder1_UltraWebTab1") == null) { 79 setTimeout("expandStep()", 1000); 80 return; 81 } 82 console.log((index++) + "打开,total:" ); 83 doExpand(); 84 if (!clicked) { 85 alert("展开完成,请下载") 86 return; 87 } else { 88 clicked = false; 89 } 90 expandStep(); 91 } 92 93 setTimeout("expandStep()", 5000);