Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python - Scrapy - Contains
#1
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
Reply
#2
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'
Reply
#3
dude....you are awesome....you have always to give the best possible answers to people.
Reply
#4
Yeah, I'm pretty amazing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Scrapy Date Extraction Issue tr8585 1 3,299 Aug-05-2020, 04:32 AM
Last Post: tr8585
  Python Scrapy tr8585 2 2,356 Aug-04-2020, 04:11 AM
Last Post: tr8585
  Python - Scrapy Baggelhsk95 0 2,281 Apr-24-2019, 01:07 PM
Last Post: Baggelhsk95
  Python Scrapy ebay API Baggelhsk95 0 3,201 Nov-21-2018, 11:22 AM
Last Post: Baggelhsk95
  Python scrapy scraped_items Baggelhsk95 2 2,876 Nov-13-2018, 08:30 AM
Last Post: Baggelhsk95
  Python - Scrapy - CSS selector Baggelhsk95 1 5,549 Nov-07-2018, 04:45 PM
Last Post: stranac
  Python - Scrapy Login in Baggelhsk95 3 4,824 Oct-23-2018, 04:24 PM
Last Post: stranac
  Python - Scrapy Ebay Test Baggelhsk95 4 4,320 Oct-16-2018, 12:37 PM
Last Post: snippsat
  Python - Scrapy Login form Baggelhsk95 4 10,770 Oct-16-2018, 08:01 AM
Last Post: Baggelhsk95
  Python - Scrapy Javascript Pagination (next_page) Baggelhsk95 3 9,989 Oct-08-2018, 01:20 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020