Scrapy列表选择器

时间:2019-03-16 13:03:16

标签: python web-scraping scrapy

我正尝试在scrapy中遍历一个列表,这是html示例:

<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
  </div>
  <ol class="jobs">      
    <li class="job ">
        <div id="logoso-listing">
            <img width="100" height="100" class="alignleft size-thumbnail wp-image-22824">             
        </div>
        <div id="titlo">
            <strong><a href="http://link/">Text1</a></strong>
        </div>
        <div id="type-tag"><span class="jtype permanent">Permanent1</span></div>
        <div id="type-tag-prev"><span class="jtype permanent">Permanent1</span></div>
    </li>
    <li class="job ">
        <div id="logoso-listing">
            <img width="100" height="100" class="alignleft size-thumbnail wp-image-22824">             
        </div>
        <div id="titlo">
            <strong><a href="http://link/">Text2</a></strong>
        </div>
        <div id="type-tag"><span class="jtype permanent">Permanent2</span></div>
        <div id="type-tag-prev"><span class="jtype permanent">Permanent2</span></div>
    </li>
    <li class="job ">
        <div id="logoso-listing">
            <img width="100" height="100" class="alignleft size-thumbnail wp-image-22824">             
        </div>
        <div id="titlo">
            <strong><a href="http://link/">Text3</a></strong>
        </div>
        <div id="type-tag"><span class="jtype permanent">Permanent3</span></div>
        <div id="type-tag-prev"><span class="jtype permanent">Permanent3</span></div>
    </li>
</ol>
 </body>
</html>

这些是抓取抓取工具的命令:

content = response.xpath("//ol[@class = 'jobs']")
job_list = content.xpath("//li[contains(@class,'job')]")
for job in job_list:
    job.xpath("//div[@id = 'titlo']/strong/a/text()").getall()

上面的代码返回:

['Text1', 'Text2', 'Text3']
['Text1', 'Text2', 'Text3']
['Text1', 'Text2', 'Text3']

我的预期输出是:

['Text1']
['Text2']
['Text3']

我没有得到什么?这种垂直的迭代,而我想以水平的方式迭代response

1 个答案:

答案 0 :(得分:0)

为什么不对带有子strong标签的a标签使用css选择器?

.css("strong a").getall()

您可能应该添加ID

#titlo strong a

对于xpath

//*[@id='titlo']/strong/a