我正试着接触Scrapy,现在我试着从一个词源网站上提取信息:http://www.etymonline.com 现在,我只想知道单词和它们的原始描述。这是一个常见的HTML代码块在etymonline中的显示方式:
<dt>
<a href="/index.php?term=address&allowed_in_frame=0">address (n.)</a>
<a href="http://dictionary.reference.com/search?q=address" class="dictionary" title="Look up address at Dictionary.com">
<img src="graphics/dictionary.gif" width="16" height="16" alt="Look up address at Dictionary.com" title="Look up address at Dictionary.com"/>
</a>
</dt>
<dd>
1530s, "dutiful or courteous approach," from <a href="/index.php?term=address&allowed_in_frame=0" class="crossreference">address</a> (v.) and from French <span class="foreign">adresse</span>. Sense of "formal speech" is from 1751. Sense of "superscription of a letter" is from 1712 and led to the meaning "place of residence" (1888).
</dd>
单词包含在<dt>
标记和下一个同级标记<dd>
中的描述中。
要在像http://www.etymonline.com/index.php?l=a&p=9&allowed_in_frame=0这样的页面上获取单词列表,可以编写word = sel.xpath('//dl/dt/a/text()').extract()
。
然后我尝试循环遍历这个单词列表,并使用这行代码info = selInfo.xpath("//dl/dt[a='"+word[i]+"']/following-sibling::dd")
提取相关信息。但似乎没用。有什么想法吗?
使用以下同级的解决方案。
基本上:
这里是输出的摘录:
要在
<dt>
之后到达<dd>
,可以使用following-sibling
轴,这是正确的。following-sibling::dd
在上下文节点后使用select alldd
元素。因此,您需要使用位置谓词[1]
,将XPath限制为只使用第一个。对于从
//dl/dt
中得到的每个dt
元素,您选择following-sibling::dd[1]
。下面是一个使用
scrapy shell
作为术语“address”的示例会话:xpath工作的思想不是
loop
提取的列表,而是在xpath的父节点中。目前我的mac电脑上没有“scrapy”,但这里的技术应该同样适用,比如:
希望这有帮助。
相关问题 更多 >
编程相关推荐