def parse_linkpage(self, response):
hxs = HtmlXPathSelector(response)
item = QualificationItem()
xpath = """
//h2[normalize-space(.)="Entry requirements for undergraduate courses"]
/following-sibling::p
"""
item['Qualification'] = hxs.select(xpath).extract()[1:]
item['Country'] = response.meta['a_of_the_link']
return item
所以我想知道是否可以让我的代码在<h2>
结束后停止刮取。只能在特定标题后才能删除内容吗?
这里是网页:
<h2>Entry requirements for undergraduate courses</h2>
<p>Example1</p>
<p>Example2</p>
<h2>Postgraduate Courses</h2>
<p>Example3</p>
<p>Example4</p>
我想这些结果:
Example1
Example2
,但我得到:
Example1
Example2
Example3
Example4
我知道我可以改变这一行,
item['Qualification'] = hxs.select(xpath).extract()
到,
item['Qualification'] = hxs.select(xpath).extract()[0:2]
但这刮看,可能有2周以上的段落在第一头这意味着它会离开这个信息了许多不同的页面。
我想知道是否有一种方法,只是告诉它提取我想要的标题后面的确切数据,而不是一切?