Python Scrapy:选择紧跟其后的一个同级,如果不存在,则返回空字符串
我有这个HTML结构:Python Scrapy:选择紧跟其后的一个同级,如果不存在,则返回空字符串,python,python-3.x,scrapy,Python,Python 3.x,Scrapy,我有这个HTML结构: <div> <h2>First title</h2> <p>First paragraph</p> <h2>Second title</h2> <h2>Third title</h2> <p>Third paragraph</p> </div> 这给了我“第一段”。但是,如果我需要选择
<div>
<h2>First title</h2>
<p>First paragraph</p>
<h2>Second title</h2>
<h2>Third title</h2>
<p>Third paragraph</p>
</div>
这给了我“第一段”
。但是,如果我需要选择第二个标题下的文本
,我会得到“第三段”
,而不是一个空字符串
有什么方法可以做到这一点吗?您得到的是
第三段“
,因为您使用的语句是在第二个标题之后第一次出现
。
您可以执行以下操作
paragraphs = []
for e in response.xpath(".//h2[contains(text(), 'title')]/following-sibling::*[1]"):
if '<p>' in e.get():
# there is a paragraph after the current title
paragraphs.append(e.xpath('.//text()')[0].get())
else:
# there is no paragraph after the current title
paragraphs.append('')
段落=[]
对于响应中的e.xpath(“.//h2[包含(text(),'title')]/以下同级::*[1]”:
如果e.get()中的“”:
#当前标题后有一段
段落.append(e.xpath('.//text()')[0].get())
其他:
#当前标题后没有段落
段落。附加(“”)
我遇到此错误AttributeError:“SelectorList”对象没有属性“getnext”
paragraphs = []
for e in response.xpath(".//h2[contains(text(), 'title')]/following-sibling::*[1]"):
if '<p>' in e.get():
# there is a paragraph after the current title
paragraphs.append(e.xpath('.//text()')[0].get())
else:
# there is no paragraph after the current title
paragraphs.append('')