我有以下元素:
<a class="html-attribute-value html-external-link" target="_blank" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ResultsGrid$15", "", false, "", "webproperty.aspx?s=id&s=15&time=201606080118012&", false, true))'>javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ucResultsGrid$R000000015&quot;, &quot;&quot;, false, &quot;&quot;, &quot;webprop.aspx?s=id&amp;sr=15&amp;time=201606080118012&amp;id=15&quot;, false, true))</a>
我想获取javascript参数以尝试重建通过单击链接生成的请求。我发现:
response.selector.xpath('//*[@id="ResultsGrid$15"]/@href').extract()
Out[20]: [u'javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ResultsGrid$15", "", false, "", "webproperty.aspx?s=id&sdata=15&time=201606080037034&id=15", false, true))']
这看起来不错并解码了javascript参数。我如何从那里抓住它们?
答案 0 :(得分:3)
您可以通过re()
method和多个捕获组执行此操作:
response.selector.xpath('//*[@id="ResultsGrid$15"]/@href').re(r'javascript:WebForm_DoPostBackWithOptions\(new WebForm_PostBackOptions\("(.*?)", "(.*?)", (.*?), "(.*?)", "(.*?)", (.*?), (.*?)\)\)')
这里我对任何字符都使用了相当广泛的.*?
- 非贪婪匹配,但你可以更严格地在哪个参数中匹配哪些字符