废话不多说!直接开始干活!
需求是三级联动菜单
找到了半天,可能不理解最后还是找到了
清除combobox数据
mainForm.combobox.clear();
完整代码
import win.ui;
import string;
import inet.whttp;
import console;
import fsys.file;
import fsys.dlg;
import thread.works
import io;
import com;
import com.excel;
import console;
import mysql.client;
//多线程管理器
import thread.manage
//创建线程管理器
manage = thread.manage(3)
//console.log("正在连接数据库服务器...");
thread.invoke( function()
while(! thread.get("db4free.net:end")){
io.stdout.write(">");sleep(100);
}
)
var dbMysql,err = mysql.client(
server = "127.0.0.1"; //数据库服务器,可省略默认为localhost
uid = "root";//用户名,可省略默认为root
pwd = "root";
);
thread.set("db4free.net:end",true)
//console.clearScreen ();
if(!dbMysql){
console.log("连接数据库失败",err);
console.pause();
return;
}
//选择数据库
dbMysql.selectDb("test")
/*DSG{{*/
mainForm = win.form(text="助手 ";right=799;bottom=599)
mainForm.add(
btnInsert={cls="button";text="导入";left=600;top=24;right=704;bottom=56;z=4};
btn_select_file={cls="button";text="选择文件";left=464;top=24;right=584;bottom=56;z=6};
button={cls="button";text="获取数据";left=458;top=139;right=538;bottom=171;z=1};
button2={cls="button";text="添加关键词";left=248;top=137;right=352;bottom=169;z=7};
button3={cls="button";text="导出";left=597;top=138;right=677;bottom=170;z=8};
button4={cls="button";text="确定";left=458;top=86;right=538;bottom=115;z=12};
button5={cls="button";text="清除";left=596;top=84;right=679;bottom=117;z=13};
combobox={cls="combobox";left=69;top=90;right=172;bottom=116;edge=1;hscroll=1;items={};mode="dropdownlist";vscroll=1;z=9};
combobox2={cls="combobox";left=187;top=89;right=291;bottom=115;edge=1;hscroll=1;items={};mode="dropdownlist";vscroll=1;z=10};
combobox3={cls="combobox";left=297;top=89;right=404;bottom=115;edge=1;hscroll=1;items={};mode="dropdownlist";vscroll=1;z=11};
edit={cls="edit";text="螺蛳粉";left=88;top=139;right=179;bottom=167;edge=1;multiline=1;z=3};
edit2={cls="edit";left=16;top=24;right=424;bottom=56;edge=1;z=5};
listview={cls="listview";left=104;top=224;right=640;bottom=504;bgcolor=12632256;border=1;edge=1;vscroll=1;z=2}
)
/*}}*/
mainForm.button5.oncommand = function(id,event){
mainForm.listview.clear();
}
mainForm.listview.insertColumn("序号",50)
mainForm.listview.insertColumn("关键词",100)
mainForm.listview.insertColumn("数量",100)
mainForm.listview.insertColumn("第一评论",100)
mainForm.listview.insertColumn("第二评论",100)
mainForm.listview.insertColumn("第三评论",100)
//mainForm.listview.insertColumn("价格1",100)
var num=0;
var filepath;
function addItem(str){
num++;
mainForm.listview.addItem( {
text={num;str;"0"}
})
}
function getResult(kwd){
import inet.whttp
import win
import console
var http = inet.whttp("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0");
var url,s
var count=mainForm.listview.count;//拿到listview数据条数
count=2;
for(i=1;count;1){
//test1=mainForm.listview.getItemText(i,1);//拿到测试1 这列数据
kwd=mainForm.listview.getItemText(i,2);//拿到关键词 这列数据
url='https://search.jd.com/Search?keyword='+kwd+'&enc=utf-8&wtype=1&click=1&qrst=1&rt=1&stop=1&vt=2&wq='++kwd;
s = http.get(url);
i2,j2 = string.find(s ,"在京东找到了");
substr = string.sub(s,i2,i2+36)
//console.log(i,j,substr,"件"++kwd++"的类")
i2,j2,v = string.find(substr ,"(\d+)");
//mainForm.listview.setItemText(kwd++v,i,3)
mainForm.listview.setItemText(v,i,3)
csv++= kwd+','++v+'\r\n'; //累加数据到csv
}
}
var obj = {
getComments=function (mainForm,i,product_ids){
//io.open();//打开控制台窗口
thread.invoke(
function(mainForm,i,product_ids){
import console;
import inet.whttp
import win
var http = inet.whttp("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0");
var url,json
for name,product_id in product_ids{
url='https://club.jd.com/comment/productCommentSummaries.action?referenceIds='++product_id;
//console.log(url)
json = http.get(url);
import web.json;
//io.print(json);
var tab = web.json.parse(json);
//io.print( tab.CommentsCount,tab.CommentsCount[1].CommentCount)
mainForm.listview.setItemText(tab.CommentsCount[1].CommentCount,i,3+name)
}
},mainForm,i,product_ids
)
}
}
function enableDpiScaling(kwd,obj){
}
function getNumber(kwd,obj){
console.log(kwd)
console.log(obj)
//io.open();//打开控制台窗口
thread.invoke(
function(mainForm,obj){
import console;
//console.log("线程在执行",thread.getId() )
import inet.whttp
import win
var csv="关键词,数量"+'\r\n'
var useagents={
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.3; rv:11.0) like Gecko";
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0"
}
useagents[5]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0";
useagents[6]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0";
useagents[3]="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0";
useagents[4]="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0";
//var http = inet.whttp(useagents[ran]);
var url,s,http,ran
var count=mainForm.listview.count;//拿到listview数据条数
//count=2;
import fsys.dlg;
import fsys.file;
var filePath=fsys.getCurDir();//获取你操作的路径
string.save(filePath+"/"+"test"+'.csv',csv)
outfile=fsys.file(filePath+"/"+"test"+'.csv',"a")
for(i=1;count;1){
ran = math.random(1, #useagents);
// console.log(useagents[ran])
http = inet.whttp(useagents[ran]);
//test1=mainForm.listview.getItemText(i,1);//拿到测试1 这列数据
kwd=mainForm.listview.getItemText(i,2);//拿到关键词 这列数据
url='https://search.jd.com/Search?keyword='+kwd+'&enc=utf-8&wtype=1&click=1&qrst=1&rt=1&stop=1&vt=2&wq='++kwd;
s = http.get(url);
i2,j2 = string.find(s ,"在京东找到了");
substr = string.sub(s,i2,i2+36)
//console.log(i,j,substr,"件"++kwd++"的类")
i2,j2,v = string.find(substr ,"(\d+)");
//mainForm.listview.setItemText(kwd++v,i,3)
mainForm.listview.setItemText(v,i,3)
outfile.write( kwd+','++v+'\r\n')
csv++= kwd+','++v+'\r\n'; //累加数据到csv
if(tonumber(v)>2){//如果有前三,找出前三
import string.xml;
var htmlDoc = string.xml( s )
var J_goodsList= htmlDoc.getEle("J_goodsList")[1][1].queryEles( tagName = "a")[1].href;
var J_goodsList2= htmlDoc.getEle("J_goodsList")[1][2].queryEles( tagName = "a")[1].href;
var J_goodsList3= htmlDoc.getEle("J_goodsList")[1][3].queryEles( tagName = "a")[1].href;
var J_goodsLists={ J_goodsList;J_goodsList2;J_goodsList3 }
i2,j2,product_id = string.find(J_goodsList ,"(\d+)");
i2,j2,product_id2 = string.find(J_goodsList2 ,"(\d+)");
i2,j2,product_id3 = string.find(J_goodsList3 ,"(\d+)");
var product_ids={product_id;product_id2;product_id3}
//console.log(product_ids);
obj.getComments(mainForm,i,product_ids);
//console.log(v)
}
sleep(810)
}
//console.log(filePath,csv)
//if(filePath){
// win.msgbox("导出到"++filePath+"test"+'.csv')
// }
},mainForm,obj
)
}
var csv="关键词,数量,第一评论,第二评论,第三评论"+'\r\n'
mainForm.button.oncommand = function(id,event){
var kwd='连衣裙';
return getNumber(kwd,obj)
var http = inet.whttp("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0");
var url,s
//console.log(url)
//console.log(string.len(s))
//i2,j2 = string.find(s ,"件"++kwd++"的类");
//console.log(i2,j2)
//listviewObject.setItemText(文本,行,列)
//mainForm.listview.setItemText(v,1,2)
//console.pause(true);
var count=mainForm.listview.count;//拿到listview数据条数
for(i=1;count;1){
//test1=mainForm.listview.getItemText(i,1);//拿到测试1 这列数据
kwd=mainForm.listview.getItemText(i,2);//拿到关键词 这列数据
console.log(kwd)
url='https://search.jd.com/Search?keyword='+kwd+'&enc=utf-8&wtype=1&click=1&qrst=1&rt=1&stop=1&vt=2&wq='++kwd;
s = http.get(url);
i2,j2 = string.find(s ,"在京东找到了");
substr = string.sub(s,i2,i2+36)
//console.log(i,j,substr,"件"++kwd++"的类")
i2,j2,v = string.find(substr ,"(\d+)");
//mainForm.listview.setItemText(kwd++v,i,3)
mainForm.listview.setItemText(v,i,3)
csv++= kwd+','++v+'\r\n'; //累加数据到csv
sleep(800)
}
//fsys.createDir('out');
//var filePath=fsys.getCurDir();//获取你操作的路径
//console.log(filePath,csv)
//if(filePath){
// string.save(filePath+"/"+"test"+'.csv',csv)
// win.msgbox("导出到"++filePath+"test"+'.csv')
// }
}
mainForm.btn_select_file.oncommand = function(id,event){
filepath = fsys.dlg.open()//打开文件夹选取窗口,并返回所选择的文件夹的路径
mainForm.edit2.text=filepath
}
mainForm.btnInsert.oncommand = function(id,event){
//mainForm.listview.setItemText("444",1,1)
//path="C:\Users\Administrator\Desktop\book\keyword.txt";
fullpath =assert( io.exist( filepath ) ,"文件不存在" )
//var excel = com.excel( true );
//var book = excel.WorkBooks.Open(fullpath);
//var sheet = excel.WorkSheets(1);
tempfile = io.open(fullpath)
line = tempfile.read();//读取下一行
//console.log(line)
//console.log(fullpath)
var nu = 0;
while( line ) {
nu ++;
bb=string.fromto(line,936,65001);
ex = string.split(bb,",");
//console.log(ex[1])
//console.log(table.unpack(ex))
//io.print(table.unpack(ex));
var box = {p1=ex[2];p2=ex[3];p3=ex[4];key=ex[1]};
if(ex[2] !='' &&ex[2]!='一级类目'){
addCombobox(ex[2]);
if(ex[3] !='' &&ex[3]!='二级类目'){
addCombobox2(ex[3]);
if(ex[4] != ''&&ex[4]!='三级类目'){
addCombobox3(ex[4]);
}
}
}
num++;
mainForm.listview.addItem( {
text={num;ex[1];"0"}
})
line = tempfile.read();//读取下一行
}
}
mainForm.button.oncommand = function(id,event){
//console.log(99)
getNumber(1,1)
}
mainForm.button2.oncommand = function(id,event){
addItem(mainForm.edit.text)
}
mainForm.button3.oncommand = function(id,event){
//fsys.createDir('out');
var filePath=fsys.getCurDir();//获取你操作的路径
//console.log(filePath,csv)
if(filePath){
string.save(filePath+"/"+"test"+'.csv',csv)
win.msgbox("导出到"++filePath+"test"+'.csv')
}
}
mainForm.combobox.oncommand = function(id,event){
//mainForm.combobox.clear();
var result = dbMysql.query("SELECT level1 FROM `cmf_role1` where 1 limit 1,7000");
for level1 in result.each(){
mainForm.combobox.add(level1)
}
mainForm.combobox2.clear();
}
mainForm.combobox2.oncommand = function(id,event){
datas = mainForm.combobox.text;
mainForm.combobox3.clear();
var result = dbMysql.query("SELECT level2 FROM `cmf_role1` where level1='"+datas+"' limit 1,7000");
for level2 in result.each(){
mainForm.combobox2.add(level2)
}
}
mainForm.combobox3.oncommand = function(id,event){
datas2 = mainForm.combobox2.text;
var result = dbMysql.query("SELECT level3 FROM `cmf_role1` where level2='"+datas2+"' limit 1,7000");
for level3 in result.each(){
mainForm.combobox3.add(level3)
}
}
mainForm.button4.oncommand = function(id,event){
mainForm.listview.clear();
datas = mainForm.combobox.text;
datas2 = mainForm.combobox2.text;
datas3 = mainForm.combobox3.text;
if(datas !="请选择"){
if(datas !="请选择"&&datas2!="请选择"&&datas3!="请选择"&&datas2!=""&&datas3!=""){
//console.log(123)
//查询数据并返回记录集
var result = dbMysql.query("SELECT name FROM `cmf_role1` where level1='"+datas+"' and level2='"+datas2+"' and level3='"+datas3+"'");
for name in result.each(){
mainForm.listview.addItem( {
text={num;name;"0"}
})
}
return
}
if(datas !="请选择"&&datas2!="请选择"&&datas2!=""){
//console.log(12)
var result = dbMysql.query("SELECT name FROM `cmf_role1` where level1='"+datas+"' and level2='"+datas2+"'");
for name in result.each(){
mainForm.listview.addItem( {
text={num;name;"0"}
})
}
return
}
var result = dbMysql.query("SELECT name FROM `cmf_role1` where level1='"+datas+"'");
//console.log(11)
for name in result.each(){
mainForm.listview.addItem( {
text={num;name;"0"}
})
}
}
}
function addCombobox(obj){
mainForm.combobox.add(obj);
}
function addCombobox2(obj){
mainForm.combobox2.add(obj);
}
function addCombobox3(obj){
mainForm.combobox3.add(obj);
}
//默认显示项设置
mainForm.combobox.add("请选择")
mainForm.combobox.selectString("请选择")//查找并默认选中
mainForm.combobox2.add("请选择")
mainForm.combobox2.selectString("请选择")//查找并默认选中
mainForm.combobox3.add("请选择")
mainForm.combobox3.selectString("请选择")//查找并默认选中
//mainForm.enableDpiScaling();
mainForm.show();
return win.loopMessage();
代码仅供参考!