假设我有这个页面:
<div class="top">
<span class="strings">asdf</span>
<span class="strings">qwer</span>
<span class="strings">zxcv</span>
</div>
<div id="content">
some other text
<span class="strings">1234</span>
<span class="strings">5678</span>
<span class="strings">1234</span>
</div>
如何让脚本只抓取 div id="content" 中的 span 类字符串,不 div class="top"?结果应为“1234”、“5678”、“1234”。
到目前为止,这是我的代码:
from lxml import html
import requests
url = 'http://www.amazon.com/dp/B00SGGQRNO'
response = requests.get(url)
tree = html.fromstring(response.content)
bullets = tree.xpath('//span[@class="strings"]/text()')
print ('Bullets: ',bullets)
请您参考如下方法:
仅选择作为 div 元素子元素的那些 span 元素(使用 @class="strings")的文本@id="content,使用这个 XPath 表达式:
//div[@id="content"]/span[@class="strings"]/text()
