列
<div id="desktop-1"> </div> # 需要匹配的div
<div id="desktop-btf-grid-1"> </div>
<div id="desktop-btf-grid-2"> </div>
<div id="desktop-2"> </div> # 需要匹配的div
<div id="desktop-btf-grid-3"> </div>
<div id="desktop-3"> </div> # 需要匹配的div
<div id="desktop-btf-grid-4"> </div>
<div id="desktop-4"> </div> # 需要匹配的div
需要获取div中id为desktop-数字的对象
一开始可能会想到用//*[contains(@id,'desktop-')]
,发现结果会把esktop-btf-grid-的数据也匹配到,这里我们就需要在xpath中使用正则
response.xpath(r'//*[re:match(@id, "desktop-\d")]',namespaces={"re": "http://exslt.org/regular-expressions"}).extract()
网友评论