How to use the Xpath and CSS selector in for function
<p>I am a rookie, and want use the scrapy framework to grab something, but I have trouble:</p>

<p>Html A:</p>

<pre><code>&lt;ul class="tip" id="tip1"&gt;
    &lt;li id="tip1_0"&gt;
        &lt;a href="http://***" title="***" target="_self"&gt;***
        &lt;/a&gt;
    &lt;/li&gt;
    &lt;li id="tip1_1"&gt;
        &lt;a href="http://***" title="***" target="_self"&gt;***
        &lt;/a&gt;
    &lt;/li&gt;
    &lt;li id="tip1_2"&gt;
        &lt;a href="http://***" title="***" target="_self"&gt;***
        &lt;/a&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>I use:</p>

<pre><code>f = response.xpath("//*[@id='tip1']//li/a/@href | //*[@id='tip1']//li/a/@title").extract()
</code></pre>

<p>When I get the f is a list, and i will change the list(f) to dict(name0=f[0], value0=f[1], name1=f[2], value1=[f3], and so on). Is any way to more easy? </p>

<p>Html B:</p>

<pre><code>&lt;div class="info"&gt;
    &lt;a target="_blank" href="***" title="***"&gt;
    &lt;/a&gt;
&lt;/div&gt;
&lt;div class="info"&gt;
    &lt;a target="_blank" href="***" title="***"&gt;
    &lt;/a&gt;
&lt;/div&gt;
&lt;div class="info"&gt;
    &lt;a target="_blank" href="***" title="***"&gt;
    &lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>In this case:</p>

<pre><code>file = response.xpath('//div[@class="info"]')
for line in file:
    f = line.xpath('/a/@href').extract()
    d = line.xpath('/a/@title').extract()
</code></pre>

<p>But, It do not work, just return 'f = []' and 'd =[]', Soï¼ i was confuse, and how can I slove this problem? Thanks a lot.</p>

<p>You could have made your inner expressions context-specific by prepending dots:</p>

<pre><code>f = line.xpath('./a/@href').extract()
d = line.xpath('./a/@title').extract()
</code></pre>

<p>Or, point your outer expression to <code>a</code> and get the <code>@href</code> and <code>@title</code>:</p>

<pre><code>file = response.xpath('//div[@class="info"]/a')
for line in file:
    f = line.xpath('@href').extract_first()
    d = line.xpath('@title').extract_first()
</code></pre>

<p>Also note the use of <code>extract_first()</code> method.</p>

python html css xpath