国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

python - how to use variables in xpath
僅有的幸福
僅有的幸福 2017-06-28 09:26:23
0
4
1790

How to use variables in xpath. I want to select elements whose id is a certain value. This value is a variable.

response.xpath('//p[@id=val]').extract_first()

The value of val is 'images', what is the syntax for using variables in xpath.

僅有的幸福
僅有的幸福

reply all(4)
洪濤

Reference article: A Concise Guide to XPATH

XPath variables use the $somevariable syntax, which is the $ symbol plus the variable name, and then the parameter variable value is passed when the xpath method is called.

>>> # `$val` used in the expression, a `val` argument needs to be passed
>>> response.xpath('//p[@id=$val]/a/text()', val='images').extract_first()
u'Name: My image 1 '
typecho
response.xpath('//p[@id={}]'.format(val)).extract_first()

I understand that the parameter of xpath is also a string, please try it.

迷茫

Scrapy Documentation

代言

This is a python statement. Why not use string splicing to splice this expression together?
For example

response.xpath('//p[@id=' + val + ']').extract_first()
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template