简体   繁体   English

如何将 javascript postData 转换为 Python 列表?

[英]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.否则检查js2xmlslimit来解析 javascript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Javascript 中没有 POSTDATA 警告的情况下重新加载页面? - How do I reload a page without a POSTDATA warning in Javascript? 如何在Javascript中没有POSTDATA警告的情况下定时刷新/重新加载页面? - How do I do a timed refresh/reload of a page without a POSTDATA warning in Javascript? 如何将 Python 代码转换为 JavaScript - How do I convert Python Code to JavaScript 如何将逗号分隔的列表转换为 <ul> 在JavaScript中列出? - How do I convert a comma separated list into a <ul>list in JavaScript? 如何转换 C# 列表<string[]>到 Javascript 阵列?</string[]> - How do I convert a C# List<string[]> to a Javascript array? 如何在JavaScript中将str转换为数组(而不是列表)? - How do I convert a str into an array (not a list) in JavaScript? 如何将第6行和第7行从python转换为javascript? - How do I convert line 6 and 7 from python into javascript? 如何将这个Python标点符号剥离功能转换为JavaScript? - How do I convert this Python punctuation-stripping function to JavaScript? 如何在 Django 模板上传递 Python 列表并在 JavaScript 中使用它? - How do I pass Python list on Django template and use it in JavaScript? 我如何在 javascript 代码中使用 python 列表? - How do i use python list inside a javascript code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM