[英]How do I convert javascript postData to Python list?
I use Scrapy to mimic Post Request from the page.我使用 Scrapy 从页面模仿 Post Request。 Need to get payload values from the following extract.
需要从以下摘录中获取有效载荷值。 I need to get the values(
postData
) from this JS construction into python list.我需要从这个 JS 构造中获取值(
postData
)到 python 列表中。
<a style="color: red;font-size: 12px;font-weight: bolder" target="_self" title="Click here for processing" onclick="return postData('714','714','null','','','TADIKONDA','0713006','TADIKONDA','','1044','EXE DNO 1046 LAND','KARLAPUDI ROSAIAH, EEDA ANJI REDDY LAND','EXE BALANCE LAND','NANDIPATI VENKATESWARLU ETC LAND','0','0','01/01/1983','25/09/2018','t','16/02/2018','1')" href="#"> Next</a>
What kind of data type is this postData
?这个
postData
是什么类型的数据?
So, what I do is as follows:所以,我所做的如下:
s = response.xpath("//td[@class = 'formbg1']/a/@onclick").extract()[0].split('Data')[1][1:-1].replace("'","").split(',')
Which returns a list.它返回一个列表。 The problem, however, is that one of the values has
,
in it, So it is broken into two separate values in a list, which is no good.然而,问题是其中一个值有
,
所以它在一个列表中被分成两个单独的值,这是不好的。 Like this one is supposed to be a single list value, but appears to be 2 values.像这个应该是单个列表值,但似乎是 2 个值。
,'KARLAPUDI ROSAIAH, EEDA ANJI REDDY LAND',
So how do convert this postData
into python list saving all values as they are?那么如何将此
postData
转换为 python 列表, postData
保存所有值?
This is no data type, it is an arbitrary javascript function defined by the page you are working with and the values here are arguments to that function which is called when the link is clicked.这不是数据类型,它是由您正在使用的页面定义的任意 javascript 函数,此处的值是单击链接时调用的该函数的参数。 You could parse it a little "by hand" to be seen as json for example like so:
您可以“手动”将其解析为 json,例如像这样:
my_list = json.loads('[' + extracted_raw_string[16:-1].replace("'", '"') + ']')
Not very robust but does the trick, will fail if there are additional double / single quotes inside the string values.不是很健壮,但如果在字符串值中有额外的双引号/单引号,则会失败。 Otherwise check js2xml or slimit for parsing javascript.
否则检查js2xml或slimit来解析 javascript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.