Python Forum
Python - Scrapy - Contains - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Python - Scrapy - Contains (/thread-13671.html)



Python - Scrapy - Contains - Baggelhsk95 - Oct-26-2018

Lets say we have this html, and we wanted to say if the tr in td contains EAN:, Print me the 2nd td in the same element

response.xpath('//strong[contains( text(), "EAN:")]/text()').extract()
<tr class="product-model">
   <td><strong>EAN:</strong></td>
   <td> 4048961089302</td>
</tr>

<tr class="product-model">
   <td><strong>Product.Nr.:</strong></td>
   <td> 83003</td>
</tr>
thank you :D


RE: Python - Scrapy - Contains - stranac - Oct-26-2018

You can use the following-sibling axis to do this:
>>> sel = scrapy.Selector(text='''<tr class="product-model">
...    <td><strong>EAN:</strong></td>
...    <td> 4048961089302</td>
... </tr>
...
... <tr class="product-model">
...    <td><strong>Product.Nr.:</strong></td>
...    <td> 83003</td>
... </tr>''')
>>> sel.xpath('//td[contains(., "EAN:")]/following-sibling::td[1]/text()').get()
' 4048961089302'



RE: Python - Scrapy - Contains - Baggelhsk95 - Oct-27-2018

dude....you are awesome....you have always to give the best possible answers to people.


RE: Python - Scrapy - Contains - stranac - Oct-27-2018

Yeah, I'm pretty amazing.