Python - Scrapy data lists
<p>I have the following piece of code in my scraper:</p>

<pre><code>import scrapy
import os
import re
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class MySpider(CrawlSpider):
    handle_httpstatus_list = [301,302,404,200,500]
    name = 'rust'
    allowed_domains = ['example.com']
    start_urls = ['http://example.com']

rules = (
    # Extract links matching 'category.php' (but not matching 'subsection.php')
    # and follow links from them (since no callback means follow=True by default).
    # Extract links matching 'item.php' and parse them with the spider's method parse_item
    Rule(LinkExtractor(), callback='parse_item', follow=True),
)

def parse_item(self, response):
 a = []
 if response.url == "http://example.com/":
  rlink = response.xpath('//a/@href').extract()
  litarget = response.xpath('//a/@target').extract()
  lirel = response.xpath('//a/@rel').extract()
  litext = response.xpath('//a/text()').extract()
  #print rlink
  for i, z in zip(rlink, litarget):
   #print i
   if i == "/some/link/":
    print z
</code></pre>

<p>This doesn't work for me :( </p>

<p>I want to achieve the following thing:
Since the extracted data is in lists: <code>rlink</code>, <code>litarget</code>, <code>lirel</code> and <code>litext</code>. I want to be able to extract corresponding information based on another one from the lists: </p>

<pre><code>if link in response.xpath('//a/@href').extract() == "/some/link"
</code></pre>

<blockquote>
  <p>print its target, rel and text attribute.</p>
</blockquote>

<p>Can you help me solve that.</p>

<p>Thanks!</p>

<p>Maybe it would be easier to grab all the <code>a</code> elements without trying to match their attributes, something like:</p>

<pre><code>for a in response.css('a'):
    if a.xpath('@href').extract_first() == 'http://some/link/':
        target = a.xpath('@target').extract_first()
        rel = a.xpath('@rel').extract_first()
        text = a.xpath('text()').extract_first()
        print target, rel, text
</code></pre>

python scrapy