我想从下面的html中提取文本,我尝试了不同的方法,但仍然失败。page_id、article_id是随机的。我想得到一个文本列表。
html:
<div id=ufi_{page_id}>
<div>
<div></div>
<div></div>
<div></div>
<div></div>
<div>
<div id={article_id}>
<div></div>
<div>I want to get the text here</div>
<div></div>
</div>
<div id={article_id2}>
<div></div>
<div>I want to get the text here</div>
<div></div>
</div>
<div id={article_id3}>
<div></div>
<div>I want to get the text here</div>
<div></div>
</div>
</div>
</div>
</div>
代码:
comments = page2.query_selector(f'xpath=//div[@id="ufi_{page_id}"]>>div>>//div[5]')
comments_ls = comments.query_selector_all("div>>//div[1]")
if comments:
for com in comments_ls:
print(com.text_content())