使用Python正则表达式提取JavaScript变量

时间:2018-11-30 10:45:30

标签: python regex

我想从网站中的javascript抓取一些数据。我已经使用Beautiful Soup来提取网页的所有评论。但是,由于找不到适合Python的良好Javascript解析器,因此我打算提取感兴趣的变量,然后最终使用eval将其转换为Python变量。但是,我不擅长正则表达式,无法编写提取JS数组的代码。

以下是数据示例:

var FirstColumnInGrid = "partnername";
var strFlow4Display = "Sell";
var strProduct4Display = "Product";

var strLanguage = "en";
var col0 = ["World","Albania","Antigua and Barbuda","Argentina","Aruba","Australia","Austria","Bahrain","Barbados","Belgium","Belize","Bermuda","Bolivia","Bosnia and Herzegovina","Brazil","Canada","Cayman Islands","Chile","China","Colombia","Cote d'Ivoire","Croatia","Cuba","Curaçao","Cyprus","Denmark","Dominica","Dominican Republic","East Asia & Pacific","Ecuador","Egypt, Arab Rep.","El Salvador","Ethiopia(excludes Eritrea)","Europe & Central Asia","Finland","France","Gabon","Georgia","Germany","Ghana","Greece","Grenada","Guam","Guatemala","Guyana","Haiti","Honduras","Hong Kong, China","India","Indonesia","North America","Norway","Oman","Other Asia, nes","Pakistan","Panama","Paraguay","Peru","Philippines","Poland","Portugal","Qatar","Romania"];
var col1 = ["2865381.57","82.56","95.26","212.68","1275.13","11229.17","2.92","39.09","4.06","211279.63","4.06","15.84","33.98","13.45","126.10","20007.63","1.21","6995.31","4220.84","1459.76","73.12","222.49","281.74","83.09","27.48","4308.36","94.65","2127.18","49178.45","352.75","43.11"];
var col2 = ["28.92","68.08","19.23","2.79","6.56","46.53","0.53","95.64","0.04","39.70","0.04","4.72","1.52","97.82","0.37","23.41","0.23","20.23","9.28","2.08","24.65","67.17","0.87","2.63","32.37","96.81","99.52","57.44","1.52","98.16","9.41","100.00","1.64","7.91","0.30","1.25","0.15","1.04","0.25","99.66","24.53","81.02","92.67","3.24","12.87","4.91","87.97","68.20","44.46","22.99","100.00","6.59","19.10","1.94","100.00","96.63","59.02","40.89"];
var col3 = ["10.34","5.98","4.60","0.02","","10.17","","2.87","0.34","10.13","0.00","0.93","","15.98","0.06","17.06","","6.34","0.20","0.29","","3.87","","","7.00","22.05","","0.17","","0.22","7.30","0.63","0.33",""];
for (var i = 0; i < 116; i++) { var row = {};row["col0"] = col0[i];
row["col1"] = col1[i];
row["col2"] = col2[i];
row["col3"] = col3[i];
row["col4"] = col4[i];
row["col5"] = col5[i];
partData[i] = row; }

我想提取数组col1, col2, ...。我用过正则表达式:

col\d = 

匹配第一部分,但直到最后],我似乎都无法匹配它。

我尝试使用:

col\d = \[*\]

但是,它说这是一个无效的表达式。

具体来说,我想将数组提取为:

col0 = ["World","Albania","Antigua and Barbuda","Argentina","Aruba","Australia","Austria","Bahrain","Barbados","Belgium","Belize","Bermuda","Bolivia","Bosnia and Herzegovina","Brazil","Canada","Cayman Islands","Chile","China","Colombia","Cote d'Ivoire","Croatia","Cuba","Curaçao","Cyprus","Denmark","Dominica","Dominican Republic","East Asia & Pacific","Ecuador","Egypt, Arab Rep.","El Salvador","Ethiopia(excludes Eritrea)","Europe & Central Asia","Finland","France","Gabon","Georgia","Germany","Ghana","Greece","Grenada","Guam","Guatemala","Guyana","Haiti","Honduras","Hong Kong, China","India","Indonesia","North America","Norway","Oman","Other Asia, nes","Pakistan","Panama","Paraguay","Peru","Philippines","Poland","Portugal","Qatar","Romania"]

以便我可以潜在地使用eval将它们转换为Python列表。

1 个答案:

答案 0 :(得分:-1)

您现在正在做的事情并不意味着正确的事情。您可能正在使用通配符*,希望匹配两个括号\[*\]之间的内容,但在正则表达式世界中,*取决于先前的模式,因此\[*表示零或更多次出现开括号。

您正在寻找的是:

col\d = \[[^\]]*\]

我将*替换为[^\]]*,这对引擎而言意味着尝试匹配除右括号之外的任何内容。

请参见live demo here